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 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 10 | // This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which |
| 11 | // pretty print the AST back out to C code. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/AST/StmtVisitor.h" |
| 16 | #include "clang/AST/Decl.h" |
Ted Kremenek | 91d1d7a | 2007-10-17 18:36:42 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprCXX.h" |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 19 | #include "clang/AST/PrettyPrinter.h" |
Chris Lattner | c7229c3 | 2007-10-07 08:58:51 +0000 | [diff] [blame] | 20 | #include "clang/Basic/IdentifierTable.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | 51221ec | 2007-11-26 22:50:46 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Streams.h" |
Chris Lattner | b0a721a | 2007-07-13 05:18:11 +0000 | [diff] [blame] | 23 | #include <iomanip> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | // StmtPrinter Visitor |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
| 30 | namespace { |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 31 | class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | std::ostream &OS; |
| 33 | unsigned IndentLevel; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 34 | clang::PrinterHelper* Helper; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | public: |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 36 | StmtPrinter(std::ostream &os, PrinterHelper* helper) : |
| 37 | OS(os), IndentLevel(0), Helper(helper) {} |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 38 | |
| 39 | void PrintStmt(Stmt *S, int SubIndent = 1) { |
| 40 | IndentLevel += SubIndent; |
| 41 | if (S && isa<Expr>(S)) { |
| 42 | // If this is an expr used in a stmt context, indent and newline it. |
| 43 | Indent(); |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 44 | Visit(S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 45 | OS << ";\n"; |
| 46 | } else if (S) { |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 47 | Visit(S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | } else { |
| 49 | Indent() << "<<<NULL STATEMENT>>>\n"; |
| 50 | } |
| 51 | IndentLevel -= SubIndent; |
| 52 | } |
| 53 | |
| 54 | void PrintRawCompoundStmt(CompoundStmt *S); |
| 55 | void PrintRawDecl(Decl *D); |
| 56 | void PrintRawIfStmt(IfStmt *If); |
| 57 | |
| 58 | void PrintExpr(Expr *E) { |
| 59 | if (E) |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 60 | Visit(E); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 61 | else |
| 62 | OS << "<null expr>"; |
| 63 | } |
| 64 | |
| 65 | std::ostream &Indent(int Delta = 0) const { |
| 66 | for (int i = 0, e = IndentLevel+Delta; i < e; ++i) |
| 67 | OS << " "; |
| 68 | return OS; |
| 69 | } |
| 70 | |
Chris Lattner | 704fe35 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 71 | bool PrintOffsetOfDesignator(Expr *E); |
| 72 | void VisitUnaryOffsetOf(UnaryOperator *Node); |
| 73 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 74 | void Visit(Stmt* S) { |
| 75 | if (Helper && Helper->handledStmt(S,OS)) |
| 76 | return; |
| 77 | else StmtVisitor<StmtPrinter>::Visit(S); |
| 78 | } |
| 79 | |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 80 | void VisitStmt(Stmt *Node); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 81 | #define STMT(N, CLASS, PARENT) \ |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 82 | void Visit##CLASS(CLASS *Node); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 83 | #include "clang/AST/StmtNodes.def" |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | //===----------------------------------------------------------------------===// |
| 88 | // Stmt printing methods. |
| 89 | //===----------------------------------------------------------------------===// |
| 90 | |
| 91 | void StmtPrinter::VisitStmt(Stmt *Node) { |
| 92 | Indent() << "<<unknown stmt type>>\n"; |
| 93 | } |
| 94 | |
| 95 | /// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and |
| 96 | /// with no newline after the }. |
| 97 | void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) { |
| 98 | OS << "{\n"; |
| 99 | for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end(); |
| 100 | I != E; ++I) |
| 101 | PrintStmt(*I); |
| 102 | |
| 103 | Indent() << "}"; |
| 104 | } |
| 105 | |
| 106 | void StmtPrinter::PrintRawDecl(Decl *D) { |
| 107 | // FIXME: Need to complete/beautify this... this code simply shows the |
| 108 | // nodes are where they need to be. |
| 109 | if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) { |
| 110 | OS << "typedef " << localType->getUnderlyingType().getAsString(); |
| 111 | OS << " " << localType->getName(); |
| 112 | } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 113 | // Emit storage class for vardecls. |
| 114 | if (VarDecl *V = dyn_cast<VarDecl>(VD)) { |
| 115 | switch (V->getStorageClass()) { |
| 116 | default: assert(0 && "Unknown storage class!"); |
| 117 | case VarDecl::None: break; |
| 118 | case VarDecl::Extern: OS << "extern "; break; |
| 119 | case VarDecl::Static: OS << "static "; break; |
| 120 | case VarDecl::Auto: OS << "auto "; break; |
| 121 | case VarDecl::Register: OS << "register "; break; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | std::string Name = VD->getName(); |
| 126 | VD->getType().getAsStringInternal(Name); |
| 127 | OS << Name; |
| 128 | |
Chris Lattner | 24c3990 | 2007-07-12 00:36:32 +0000 | [diff] [blame] | 129 | // If this is a vardecl with an initializer, emit it. |
| 130 | if (VarDecl *V = dyn_cast<VarDecl>(VD)) { |
| 131 | if (V->getInit()) { |
| 132 | OS << " = "; |
| 133 | PrintExpr(V->getInit()); |
| 134 | } |
| 135 | } |
Steve Naroff | 91578f3 | 2007-11-17 21:21:01 +0000 | [diff] [blame] | 136 | } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 137 | // print a free standing tag decl (e.g. "struct x;"). |
| 138 | OS << TD->getKindName(); |
| 139 | OS << " "; |
| 140 | if (const IdentifierInfo *II = TD->getIdentifier()) |
| 141 | OS << II->getName(); |
| 142 | else |
| 143 | OS << "<anonymous>"; |
| 144 | // FIXME: print tag bodies. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 145 | } else { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 146 | assert(0 && "Unexpected decl"); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | |
| 151 | void StmtPrinter::VisitNullStmt(NullStmt *Node) { |
| 152 | Indent() << ";\n"; |
| 153 | } |
| 154 | |
| 155 | void StmtPrinter::VisitDeclStmt(DeclStmt *Node) { |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 156 | for (ScopedDecl *D = Node->getDecl(); D; D = D->getNextDeclarator()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 157 | Indent(); |
| 158 | PrintRawDecl(D); |
| 159 | OS << ";\n"; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) { |
| 164 | Indent(); |
| 165 | PrintRawCompoundStmt(Node); |
| 166 | OS << "\n"; |
| 167 | } |
| 168 | |
| 169 | void StmtPrinter::VisitCaseStmt(CaseStmt *Node) { |
| 170 | Indent(-1) << "case "; |
| 171 | PrintExpr(Node->getLHS()); |
| 172 | if (Node->getRHS()) { |
| 173 | OS << " ... "; |
| 174 | PrintExpr(Node->getRHS()); |
| 175 | } |
| 176 | OS << ":\n"; |
| 177 | |
| 178 | PrintStmt(Node->getSubStmt(), 0); |
| 179 | } |
| 180 | |
| 181 | void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) { |
| 182 | Indent(-1) << "default:\n"; |
| 183 | PrintStmt(Node->getSubStmt(), 0); |
| 184 | } |
| 185 | |
| 186 | void StmtPrinter::VisitLabelStmt(LabelStmt *Node) { |
| 187 | Indent(-1) << Node->getName() << ":\n"; |
| 188 | PrintStmt(Node->getSubStmt(), 0); |
| 189 | } |
| 190 | |
| 191 | void StmtPrinter::PrintRawIfStmt(IfStmt *If) { |
| 192 | OS << "if "; |
| 193 | PrintExpr(If->getCond()); |
| 194 | |
| 195 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) { |
| 196 | OS << ' '; |
| 197 | PrintRawCompoundStmt(CS); |
| 198 | OS << (If->getElse() ? ' ' : '\n'); |
| 199 | } else { |
| 200 | OS << '\n'; |
| 201 | PrintStmt(If->getThen()); |
| 202 | if (If->getElse()) Indent(); |
| 203 | } |
| 204 | |
| 205 | if (Stmt *Else = If->getElse()) { |
| 206 | OS << "else"; |
| 207 | |
| 208 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) { |
| 209 | OS << ' '; |
| 210 | PrintRawCompoundStmt(CS); |
| 211 | OS << '\n'; |
| 212 | } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) { |
| 213 | OS << ' '; |
| 214 | PrintRawIfStmt(ElseIf); |
| 215 | } else { |
| 216 | OS << '\n'; |
| 217 | PrintStmt(If->getElse()); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | void StmtPrinter::VisitIfStmt(IfStmt *If) { |
| 223 | Indent(); |
| 224 | PrintRawIfStmt(If); |
| 225 | } |
| 226 | |
| 227 | void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) { |
| 228 | Indent() << "switch ("; |
| 229 | PrintExpr(Node->getCond()); |
| 230 | OS << ")"; |
| 231 | |
| 232 | // Pretty print compoundstmt bodies (very common). |
| 233 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { |
| 234 | OS << " "; |
| 235 | PrintRawCompoundStmt(CS); |
| 236 | OS << "\n"; |
| 237 | } else { |
| 238 | OS << "\n"; |
| 239 | PrintStmt(Node->getBody()); |
| 240 | } |
| 241 | } |
| 242 | |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 243 | void StmtPrinter::VisitSwitchCase(SwitchCase*) { |
| 244 | assert(0 && "SwitchCase is an abstract class"); |
| 245 | } |
| 246 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 247 | void StmtPrinter::VisitWhileStmt(WhileStmt *Node) { |
| 248 | Indent() << "while ("; |
| 249 | PrintExpr(Node->getCond()); |
| 250 | OS << ")\n"; |
| 251 | PrintStmt(Node->getBody()); |
| 252 | } |
| 253 | |
| 254 | void StmtPrinter::VisitDoStmt(DoStmt *Node) { |
Chris Lattner | 8bdcc47 | 2007-09-15 21:49:37 +0000 | [diff] [blame] | 255 | Indent() << "do "; |
| 256 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { |
| 257 | PrintRawCompoundStmt(CS); |
| 258 | OS << " "; |
| 259 | } else { |
| 260 | OS << "\n"; |
| 261 | PrintStmt(Node->getBody()); |
| 262 | Indent(); |
| 263 | } |
| 264 | |
| 265 | OS << "while "; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 266 | PrintExpr(Node->getCond()); |
| 267 | OS << ";\n"; |
| 268 | } |
| 269 | |
| 270 | void StmtPrinter::VisitForStmt(ForStmt *Node) { |
| 271 | Indent() << "for ("; |
| 272 | if (Node->getInit()) { |
| 273 | if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit())) |
| 274 | PrintRawDecl(DS->getDecl()); |
| 275 | else |
| 276 | PrintExpr(cast<Expr>(Node->getInit())); |
| 277 | } |
Chris Lattner | 8bdcc47 | 2007-09-15 21:49:37 +0000 | [diff] [blame] | 278 | OS << ";"; |
| 279 | if (Node->getCond()) { |
| 280 | OS << " "; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 281 | PrintExpr(Node->getCond()); |
Chris Lattner | 8bdcc47 | 2007-09-15 21:49:37 +0000 | [diff] [blame] | 282 | } |
| 283 | OS << ";"; |
| 284 | if (Node->getInc()) { |
| 285 | OS << " "; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 286 | PrintExpr(Node->getInc()); |
Chris Lattner | 8bdcc47 | 2007-09-15 21:49:37 +0000 | [diff] [blame] | 287 | } |
| 288 | OS << ") "; |
| 289 | |
| 290 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { |
| 291 | PrintRawCompoundStmt(CS); |
| 292 | OS << "\n"; |
| 293 | } else { |
| 294 | OS << "\n"; |
| 295 | PrintStmt(Node->getBody()); |
| 296 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 299 | void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) { |
Fariborz Jahanian | 0196cab | 2008-01-02 22:54:34 +0000 | [diff] [blame] | 300 | Indent() << "for ("; |
| 301 | if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement())) |
| 302 | PrintRawDecl(DS->getDecl()); |
| 303 | else |
| 304 | PrintExpr(cast<Expr>(Node->getElement())); |
| 305 | OS << " in "; |
| 306 | PrintExpr(Node->getCollection()); |
| 307 | OS << ") "; |
| 308 | |
| 309 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { |
| 310 | PrintRawCompoundStmt(CS); |
| 311 | OS << "\n"; |
| 312 | } else { |
| 313 | OS << "\n"; |
| 314 | PrintStmt(Node->getBody()); |
| 315 | } |
| 316 | } |
| 317 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 318 | void StmtPrinter::VisitGotoStmt(GotoStmt *Node) { |
| 319 | Indent() << "goto " << Node->getLabel()->getName() << ";\n"; |
| 320 | } |
| 321 | |
| 322 | void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) { |
| 323 | Indent() << "goto *"; |
| 324 | PrintExpr(Node->getTarget()); |
| 325 | OS << ";\n"; |
| 326 | } |
| 327 | |
| 328 | void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) { |
| 329 | Indent() << "continue;\n"; |
| 330 | } |
| 331 | |
| 332 | void StmtPrinter::VisitBreakStmt(BreakStmt *Node) { |
| 333 | Indent() << "break;\n"; |
| 334 | } |
| 335 | |
| 336 | |
| 337 | void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) { |
| 338 | Indent() << "return"; |
| 339 | if (Node->getRetValue()) { |
| 340 | OS << " "; |
| 341 | PrintExpr(Node->getRetValue()); |
| 342 | } |
| 343 | OS << ";\n"; |
| 344 | } |
| 345 | |
Chris Lattner | fe79595 | 2007-10-29 04:04:16 +0000 | [diff] [blame] | 346 | |
| 347 | void StmtPrinter::VisitAsmStmt(AsmStmt *Node) { |
Anders Carlsson | 39c47b5 | 2007-11-23 23:12:25 +0000 | [diff] [blame] | 348 | Indent() << "asm "; |
| 349 | |
| 350 | if (Node->isVolatile()) |
| 351 | OS << "volatile "; |
| 352 | |
| 353 | OS << "("; |
Anders Carlsson | 6a0ef4b | 2007-11-20 19:21:03 +0000 | [diff] [blame] | 354 | VisitStringLiteral(Node->getAsmString()); |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 355 | |
| 356 | // Outputs |
| 357 | if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 || |
| 358 | Node->getNumClobbers() != 0) |
| 359 | OS << " : "; |
| 360 | |
| 361 | for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) { |
| 362 | if (i != 0) |
| 363 | OS << ", "; |
| 364 | |
| 365 | if (!Node->getOutputName(i).empty()) { |
| 366 | OS << '['; |
| 367 | OS << Node->getOutputName(i); |
| 368 | OS << "] "; |
| 369 | } |
| 370 | |
| 371 | VisitStringLiteral(Node->getOutputConstraint(i)); |
| 372 | OS << " "; |
| 373 | Visit(Node->getOutputExpr(i)); |
| 374 | } |
| 375 | |
| 376 | // Inputs |
| 377 | if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0) |
| 378 | OS << " : "; |
| 379 | |
| 380 | for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) { |
| 381 | if (i != 0) |
| 382 | OS << ", "; |
| 383 | |
| 384 | if (!Node->getInputName(i).empty()) { |
| 385 | OS << '['; |
| 386 | OS << Node->getInputName(i); |
| 387 | OS << "] "; |
| 388 | } |
| 389 | |
| 390 | VisitStringLiteral(Node->getInputConstraint(i)); |
| 391 | OS << " "; |
| 392 | Visit(Node->getInputExpr(i)); |
| 393 | } |
| 394 | |
| 395 | // Clobbers |
| 396 | if (Node->getNumClobbers() != 0) |
| 397 | OS << " : "; |
| 398 | |
| 399 | for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) { |
| 400 | if (i != 0) |
| 401 | OS << ", "; |
| 402 | |
| 403 | VisitStringLiteral(Node->getClobber(i)); |
| 404 | } |
| 405 | |
Anders Carlsson | 6a0ef4b | 2007-11-20 19:21:03 +0000 | [diff] [blame] | 406 | OS << ");\n"; |
Chris Lattner | fe79595 | 2007-10-29 04:04:16 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 409 | void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) { |
Fariborz Jahanian | 7794cb8 | 2007-11-02 18:16:07 +0000 | [diff] [blame] | 410 | Indent() << "@try"; |
| 411 | if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) { |
| 412 | PrintRawCompoundStmt(TS); |
| 413 | OS << "\n"; |
| 414 | } |
| 415 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 416 | for (ObjCAtCatchStmt *catchStmt = |
| 417 | static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts()); |
Fariborz Jahanian | 7794cb8 | 2007-11-02 18:16:07 +0000 | [diff] [blame] | 418 | catchStmt; |
| 419 | catchStmt = |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 420 | static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) { |
Fariborz Jahanian | 7794cb8 | 2007-11-02 18:16:07 +0000 | [diff] [blame] | 421 | Indent() << "@catch("; |
| 422 | if (catchStmt->getCatchParamStmt()) { |
| 423 | if (DeclStmt *DS = dyn_cast<DeclStmt>(catchStmt->getCatchParamStmt())) |
| 424 | PrintRawDecl(DS->getDecl()); |
| 425 | } |
| 426 | OS << ")"; |
| 427 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) |
| 428 | { |
| 429 | PrintRawCompoundStmt(CS); |
| 430 | OS << "\n"; |
| 431 | } |
| 432 | } |
| 433 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 434 | if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>( |
Fariborz Jahanian | 1e7eab4 | 2007-11-07 00:46:42 +0000 | [diff] [blame] | 435 | Node->getFinallyStmt())) { |
| 436 | Indent() << "@finally"; |
| 437 | PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody())); |
Fariborz Jahanian | 7794cb8 | 2007-11-02 18:16:07 +0000 | [diff] [blame] | 438 | OS << "\n"; |
| 439 | } |
Fariborz Jahanian | b210bd0 | 2007-11-01 21:12:44 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 442 | void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) { |
Fariborz Jahanian | b210bd0 | 2007-11-01 21:12:44 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 445 | void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) { |
Fariborz Jahanian | b210bd0 | 2007-11-01 21:12:44 +0000 | [diff] [blame] | 446 | Indent() << "@catch (...) { /* todo */ } \n"; |
| 447 | } |
| 448 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 449 | void StmtPrinter::VisitObjCAtThrowStmt (ObjCAtThrowStmt *Node) { |
Fariborz Jahanian | 39f8f15 | 2007-11-07 02:00:49 +0000 | [diff] [blame] | 450 | Indent() << "@throw"; |
| 451 | if (Node->getThrowExpr()) { |
| 452 | OS << " "; |
| 453 | PrintExpr(Node->getThrowExpr()); |
| 454 | } |
| 455 | OS << ";\n"; |
| 456 | } |
| 457 | |
Fariborz Jahanian | c385c90 | 2008-01-29 18:21:32 +0000 | [diff] [blame^] | 458 | void StmtPrinter::VisitObjCAtSynchronizedStmt (ObjCAtSynchronizedStmt *Node) { |
| 459 | Indent() << "@synchronized ("; |
| 460 | PrintExpr(Node->getSynchExpr()); |
| 461 | OS << ")"; |
| 462 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getSynchBody())) |
| 463 | { |
| 464 | PrintRawCompoundStmt(CS); |
| 465 | OS << "\n"; |
| 466 | } |
| 467 | } |
| 468 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 469 | //===----------------------------------------------------------------------===// |
| 470 | // Expr printing methods. |
| 471 | //===----------------------------------------------------------------------===// |
| 472 | |
| 473 | void StmtPrinter::VisitExpr(Expr *Node) { |
| 474 | OS << "<<unknown expr type>>"; |
| 475 | } |
| 476 | |
| 477 | void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) { |
| 478 | OS << Node->getDecl()->getName(); |
| 479 | } |
| 480 | |
Steve Naroff | 7779db4 | 2007-11-12 14:29:37 +0000 | [diff] [blame] | 481 | void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { |
Fariborz Jahanian | 232220c | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 482 | if (Node->getBase()) { |
| 483 | PrintExpr(Node->getBase()); |
| 484 | OS << (Node->isArrow() ? "->" : "."); |
| 485 | } |
Steve Naroff | 7779db4 | 2007-11-12 14:29:37 +0000 | [diff] [blame] | 486 | OS << Node->getDecl()->getName(); |
| 487 | } |
| 488 | |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 489 | void StmtPrinter::VisitPreDefinedExpr(PreDefinedExpr *Node) { |
| 490 | switch (Node->getIdentType()) { |
| 491 | default: |
| 492 | assert(0 && "unknown case"); |
| 493 | case PreDefinedExpr::Func: |
| 494 | OS << "__func__"; |
| 495 | break; |
| 496 | case PreDefinedExpr::Function: |
| 497 | OS << "__FUNCTION__"; |
| 498 | break; |
| 499 | case PreDefinedExpr::PrettyFunction: |
| 500 | OS << "__PRETTY_FUNCTION__"; |
| 501 | break; |
| 502 | } |
| 503 | } |
| 504 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 505 | void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) { |
Chris Lattner | 8bf9f07 | 2007-07-13 23:58:20 +0000 | [diff] [blame] | 506 | // FIXME should print an L for wchar_t constants |
Chris Lattner | b0a721a | 2007-07-13 05:18:11 +0000 | [diff] [blame] | 507 | unsigned value = Node->getValue(); |
Chris Lattner | 8bf9f07 | 2007-07-13 23:58:20 +0000 | [diff] [blame] | 508 | switch (value) { |
| 509 | case '\\': |
| 510 | OS << "'\\\\'"; |
| 511 | break; |
| 512 | case '\'': |
| 513 | OS << "'\\''"; |
| 514 | break; |
| 515 | case '\a': |
| 516 | // TODO: K&R: the meaning of '\\a' is different in traditional C |
| 517 | OS << "'\\a'"; |
| 518 | break; |
| 519 | case '\b': |
| 520 | OS << "'\\b'"; |
| 521 | break; |
| 522 | // Nonstandard escape sequence. |
| 523 | /*case '\e': |
| 524 | OS << "'\\e'"; |
| 525 | break;*/ |
| 526 | case '\f': |
| 527 | OS << "'\\f'"; |
| 528 | break; |
| 529 | case '\n': |
| 530 | OS << "'\\n'"; |
| 531 | break; |
| 532 | case '\r': |
| 533 | OS << "'\\r'"; |
| 534 | break; |
| 535 | case '\t': |
| 536 | OS << "'\\t'"; |
| 537 | break; |
| 538 | case '\v': |
| 539 | OS << "'\\v'"; |
| 540 | break; |
| 541 | default: |
| 542 | if (isprint(value) && value < 256) { |
| 543 | OS << "'" << (char)value << "'"; |
| 544 | } else if (value < 256) { |
| 545 | OS << "'\\x" << std::hex << value << std::dec << "'"; |
| 546 | } else { |
| 547 | // FIXME what to really do here? |
| 548 | OS << value; |
| 549 | } |
Chris Lattner | b0a721a | 2007-07-13 05:18:11 +0000 | [diff] [blame] | 550 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) { |
| 554 | bool isSigned = Node->getType()->isSignedIntegerType(); |
| 555 | OS << Node->getValue().toString(10, isSigned); |
| 556 | |
| 557 | // Emit suffixes. Integer literals are always a builtin integer type. |
| 558 | switch (cast<BuiltinType>(Node->getType().getCanonicalType())->getKind()) { |
| 559 | default: assert(0 && "Unexpected type for integer literal!"); |
| 560 | case BuiltinType::Int: break; // no suffix. |
| 561 | case BuiltinType::UInt: OS << 'U'; break; |
| 562 | case BuiltinType::Long: OS << 'L'; break; |
| 563 | case BuiltinType::ULong: OS << "UL"; break; |
| 564 | case BuiltinType::LongLong: OS << "LL"; break; |
| 565 | case BuiltinType::ULongLong: OS << "ULL"; break; |
| 566 | } |
| 567 | } |
| 568 | void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) { |
Chris Lattner | 86e499d | 2007-08-01 00:23:58 +0000 | [diff] [blame] | 569 | // FIXME: print value more precisely. |
Chris Lattner | c9bec4b | 2007-09-22 18:47:25 +0000 | [diff] [blame] | 570 | OS << Node->getValueAsDouble(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 571 | } |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 572 | |
| 573 | void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) { |
| 574 | PrintExpr(Node->getSubExpr()); |
| 575 | OS << "i"; |
| 576 | } |
| 577 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 578 | void StmtPrinter::VisitStringLiteral(StringLiteral *Str) { |
| 579 | if (Str->isWide()) OS << 'L'; |
| 580 | OS << '"'; |
Anders Carlsson | ee98ac5 | 2007-10-15 02:50:23 +0000 | [diff] [blame] | 581 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 582 | // FIXME: this doesn't print wstrings right. |
| 583 | for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) { |
| 584 | switch (Str->getStrData()[i]) { |
| 585 | default: OS << Str->getStrData()[i]; break; |
| 586 | // Handle some common ones to make dumps prettier. |
| 587 | case '\\': OS << "\\\\"; break; |
| 588 | case '"': OS << "\\\""; break; |
| 589 | case '\n': OS << "\\n"; break; |
| 590 | case '\t': OS << "\\t"; break; |
| 591 | case '\a': OS << "\\a"; break; |
| 592 | case '\b': OS << "\\b"; break; |
| 593 | } |
| 594 | } |
| 595 | OS << '"'; |
| 596 | } |
| 597 | void StmtPrinter::VisitParenExpr(ParenExpr *Node) { |
| 598 | OS << "("; |
| 599 | PrintExpr(Node->getSubExpr()); |
| 600 | OS << ")"; |
| 601 | } |
| 602 | void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) { |
Chris Lattner | 296bf19 | 2007-08-23 21:46:40 +0000 | [diff] [blame] | 603 | if (!Node->isPostfix()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 604 | OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); |
Chris Lattner | 296bf19 | 2007-08-23 21:46:40 +0000 | [diff] [blame] | 605 | |
| 606 | // Print a space if this is an "identifier operator" like sizeof or __real. |
| 607 | switch (Node->getOpcode()) { |
| 608 | default: break; |
| 609 | case UnaryOperator::SizeOf: |
| 610 | case UnaryOperator::AlignOf: |
| 611 | case UnaryOperator::Real: |
| 612 | case UnaryOperator::Imag: |
| 613 | case UnaryOperator::Extension: |
| 614 | OS << ' '; |
| 615 | break; |
| 616 | } |
| 617 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 618 | PrintExpr(Node->getSubExpr()); |
| 619 | |
| 620 | if (Node->isPostfix()) |
| 621 | OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 622 | } |
Chris Lattner | 704fe35 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 623 | |
| 624 | bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) { |
| 625 | if (isa<CompoundLiteralExpr>(E)) { |
| 626 | // Base case, print the type and comma. |
| 627 | OS << E->getType().getAsString() << ", "; |
| 628 | return true; |
| 629 | } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) { |
| 630 | PrintOffsetOfDesignator(ASE->getLHS()); |
| 631 | OS << "["; |
| 632 | PrintExpr(ASE->getRHS()); |
| 633 | OS << "]"; |
| 634 | return false; |
| 635 | } else { |
| 636 | MemberExpr *ME = cast<MemberExpr>(E); |
| 637 | bool IsFirst = PrintOffsetOfDesignator(ME->getBase()); |
| 638 | OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getName(); |
| 639 | return false; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) { |
| 644 | OS << "__builtin_offsetof("; |
| 645 | PrintOffsetOfDesignator(Node->getSubExpr()); |
| 646 | OS << ")"; |
| 647 | } |
| 648 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 649 | void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) { |
| 650 | OS << (Node->isSizeOf() ? "sizeof(" : "__alignof("); |
| 651 | OS << Node->getArgumentType().getAsString() << ")"; |
| 652 | } |
| 653 | void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) { |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 654 | PrintExpr(Node->getLHS()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 655 | OS << "["; |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 656 | PrintExpr(Node->getRHS()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 657 | OS << "]"; |
| 658 | } |
| 659 | |
| 660 | void StmtPrinter::VisitCallExpr(CallExpr *Call) { |
| 661 | PrintExpr(Call->getCallee()); |
| 662 | OS << "("; |
| 663 | for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) { |
| 664 | if (i) OS << ", "; |
| 665 | PrintExpr(Call->getArg(i)); |
| 666 | } |
| 667 | OS << ")"; |
| 668 | } |
| 669 | void StmtPrinter::VisitMemberExpr(MemberExpr *Node) { |
| 670 | PrintExpr(Node->getBase()); |
| 671 | OS << (Node->isArrow() ? "->" : "."); |
| 672 | |
| 673 | FieldDecl *Field = Node->getMemberDecl(); |
| 674 | assert(Field && "MemberExpr should alway reference a field!"); |
| 675 | OS << Field->getName(); |
| 676 | } |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 677 | void StmtPrinter::VisitOCUVectorElementExpr(OCUVectorElementExpr *Node) { |
Steve Naroff | 31a4584 | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 678 | PrintExpr(Node->getBase()); |
| 679 | OS << "."; |
| 680 | OS << Node->getAccessor().getName(); |
| 681 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 682 | void StmtPrinter::VisitCastExpr(CastExpr *Node) { |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 683 | OS << "(" << Node->getType().getAsString() << ")"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 684 | PrintExpr(Node->getSubExpr()); |
| 685 | } |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 686 | void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) { |
| 687 | OS << "(" << Node->getType().getAsString() << ")"; |
| 688 | PrintExpr(Node->getInitializer()); |
| 689 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 690 | void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) { |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 691 | // No need to print anything, simply forward to the sub expression. |
| 692 | PrintExpr(Node->getSubExpr()); |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 693 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 694 | void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) { |
| 695 | PrintExpr(Node->getLHS()); |
| 696 | OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; |
| 697 | PrintExpr(Node->getRHS()); |
| 698 | } |
Chris Lattner | eb14fe8 | 2007-08-25 02:00:02 +0000 | [diff] [blame] | 699 | void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) { |
| 700 | PrintExpr(Node->getLHS()); |
| 701 | OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; |
| 702 | PrintExpr(Node->getRHS()); |
| 703 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 704 | void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { |
| 705 | PrintExpr(Node->getCond()); |
Ted Kremenek | 8e911c4 | 2007-11-26 18:27:54 +0000 | [diff] [blame] | 706 | |
| 707 | if (Node->getLHS()) { |
| 708 | OS << " ? "; |
| 709 | PrintExpr(Node->getLHS()); |
| 710 | OS << " : "; |
| 711 | } |
| 712 | else { // Handle GCC extention where LHS can be NULL. |
| 713 | OS << " ?: "; |
| 714 | } |
| 715 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 716 | PrintExpr(Node->getRHS()); |
| 717 | } |
| 718 | |
| 719 | // GNU extensions. |
| 720 | |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 721 | void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 722 | OS << "&&" << Node->getLabel()->getName(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 725 | void StmtPrinter::VisitStmtExpr(StmtExpr *E) { |
| 726 | OS << "("; |
| 727 | PrintRawCompoundStmt(E->getSubStmt()); |
| 728 | OS << ")"; |
| 729 | } |
| 730 | |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 731 | void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) { |
| 732 | OS << "__builtin_types_compatible_p("; |
| 733 | OS << Node->getArgType1().getAsString() << ","; |
| 734 | OS << Node->getArgType2().getAsString() << ")"; |
| 735 | } |
| 736 | |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 737 | void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) { |
| 738 | OS << "__builtin_choose_expr("; |
| 739 | PrintExpr(Node->getCond()); |
Chris Lattner | 94f05e3 | 2007-08-04 00:20:15 +0000 | [diff] [blame] | 740 | OS << ", "; |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 741 | PrintExpr(Node->getLHS()); |
Chris Lattner | 94f05e3 | 2007-08-04 00:20:15 +0000 | [diff] [blame] | 742 | OS << ", "; |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 743 | PrintExpr(Node->getRHS()); |
| 744 | OS << ")"; |
| 745 | } |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 746 | |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 747 | void StmtPrinter::VisitOverloadExpr(OverloadExpr *Node) { |
| 748 | OS << "__builtin_overload("; |
| 749 | for (unsigned i = 0, e = Node->getNumArgs(); i != e; ++i) { |
| 750 | if (i) OS << ", "; |
| 751 | PrintExpr(Node->getArg(i)); |
| 752 | } |
| 753 | OS << ")"; |
| 754 | } |
| 755 | |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 756 | void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { |
| 757 | OS << "{ "; |
| 758 | for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { |
| 759 | if (i) OS << ", "; |
| 760 | PrintExpr(Node->getInit(i)); |
| 761 | } |
| 762 | OS << " }"; |
| 763 | } |
| 764 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 765 | void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) { |
| 766 | OS << "va_arg("; |
| 767 | PrintExpr(Node->getSubExpr()); |
| 768 | OS << ", "; |
| 769 | OS << Node->getType().getAsString(); |
| 770 | OS << ")"; |
| 771 | } |
| 772 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 773 | // C++ |
| 774 | |
| 775 | void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) { |
Chris Lattner | 36460ee | 2007-08-09 17:34:19 +0000 | [diff] [blame] | 776 | OS << CXXCastExpr::getOpcodeStr(Node->getOpcode()) << '<'; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 777 | OS << Node->getDestType().getAsString() << ">("; |
| 778 | PrintExpr(Node->getSubExpr()); |
| 779 | OS << ")"; |
| 780 | } |
| 781 | |
| 782 | void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { |
| 783 | OS << (Node->getValue() ? "true" : "false"); |
| 784 | } |
| 785 | |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 786 | // Obj-C |
| 787 | |
| 788 | void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { |
| 789 | OS << "@"; |
| 790 | VisitStringLiteral(Node->getString()); |
| 791 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 792 | |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 793 | void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { |
Chris Lattner | 994f939 | 2007-10-18 00:39:29 +0000 | [diff] [blame] | 794 | OS << "@encode(" << Node->getEncodedType().getAsString() << ")"; |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 797 | void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) { |
Chris Lattner | 994f939 | 2007-10-18 00:39:29 +0000 | [diff] [blame] | 798 | OS << "@selector(" << Node->getSelector().getName() << ")"; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 801 | void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) { |
Chris Lattner | 994f939 | 2007-10-18 00:39:29 +0000 | [diff] [blame] | 802 | OS << "@protocol(" << Node->getProtocol()->getName() << ")"; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 805 | void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) { |
| 806 | OS << "["; |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 807 | Expr *receiver = Mess->getReceiver(); |
| 808 | if (receiver) PrintExpr(receiver); |
| 809 | else OS << Mess->getClassName()->getName(); |
| 810 | Selector &selector = Mess->getSelector(); |
| 811 | if (selector.isUnarySelector()) { |
| 812 | OS << " " << selector.getIdentifierInfoForSlot(0)->getName(); |
| 813 | } else { |
| 814 | for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) { |
Fariborz Jahanian | f24d95a | 2007-10-16 20:52:13 +0000 | [diff] [blame] | 815 | if (selector.getIdentifierInfoForSlot(i)) |
| 816 | OS << selector.getIdentifierInfoForSlot(i)->getName() << ":"; |
| 817 | else |
| 818 | OS << ":"; |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 819 | PrintExpr(Mess->getArg(i)); |
| 820 | } |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 821 | } |
| 822 | OS << "]"; |
| 823 | } |
| 824 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 825 | //===----------------------------------------------------------------------===// |
| 826 | // Stmt method implementations |
| 827 | //===----------------------------------------------------------------------===// |
| 828 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 829 | void Stmt::dumpPretty() const { |
Ted Kremenek | 51221ec | 2007-11-26 22:50:46 +0000 | [diff] [blame] | 830 | printPretty(*llvm::cerr.stream()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 833 | void Stmt::printPretty(std::ostream &OS, PrinterHelper* Helper) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 834 | if (this == 0) { |
| 835 | OS << "<NULL>"; |
| 836 | return; |
| 837 | } |
| 838 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 839 | StmtPrinter P(OS, Helper); |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 840 | P.Visit(const_cast<Stmt*>(this)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 841 | } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 842 | |
| 843 | //===----------------------------------------------------------------------===// |
| 844 | // PrinterHelper |
| 845 | //===----------------------------------------------------------------------===// |
| 846 | |
| 847 | // Implement virtual destructor. |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 848 | PrinterHelper::~PrinterHelper() {} |