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