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 | // |
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" |
| 17 | #include "clang/AST/ExprCXX.h" |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 18 | #include "clang/AST/PrettyPrinter.h" |
Chris Lattner | c7229c3 | 2007-10-07 08:58:51 +0000 | [diff] [blame] | 19 | #include "clang/Basic/IdentifierTable.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compiler.h" |
| 21 | #include <iostream> |
Chris Lattner | b0a721a | 2007-07-13 05:18:11 +0000 | [diff] [blame] | 22 | #include <iomanip> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | // StmtPrinter Visitor |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | namespace { |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 30 | class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | std::ostream &OS; |
| 32 | unsigned IndentLevel; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 33 | clang::PrinterHelper* Helper; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | public: |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 35 | StmtPrinter(std::ostream &os, PrinterHelper* helper) : |
| 36 | OS(os), IndentLevel(0), Helper(helper) {} |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 37 | |
| 38 | void PrintStmt(Stmt *S, int SubIndent = 1) { |
| 39 | IndentLevel += SubIndent; |
| 40 | if (S && isa<Expr>(S)) { |
| 41 | // If this is an expr used in a stmt context, indent and newline it. |
| 42 | Indent(); |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 43 | Visit(S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 44 | OS << ";\n"; |
| 45 | } else if (S) { |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 46 | Visit(S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | } else { |
| 48 | Indent() << "<<<NULL STATEMENT>>>\n"; |
| 49 | } |
| 50 | IndentLevel -= SubIndent; |
| 51 | } |
| 52 | |
| 53 | void PrintRawCompoundStmt(CompoundStmt *S); |
| 54 | void PrintRawDecl(Decl *D); |
| 55 | void PrintRawIfStmt(IfStmt *If); |
| 56 | |
| 57 | void PrintExpr(Expr *E) { |
| 58 | if (E) |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 59 | Visit(E); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | else |
| 61 | OS << "<null expr>"; |
| 62 | } |
| 63 | |
| 64 | std::ostream &Indent(int Delta = 0) const { |
| 65 | for (int i = 0, e = IndentLevel+Delta; i < e; ++i) |
| 66 | OS << " "; |
| 67 | return OS; |
| 68 | } |
| 69 | |
Chris Lattner | 704fe35 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 70 | bool PrintOffsetOfDesignator(Expr *E); |
| 71 | void VisitUnaryOffsetOf(UnaryOperator *Node); |
| 72 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 73 | void Visit(Stmt* S) { |
| 74 | if (Helper && Helper->handledStmt(S,OS)) |
| 75 | return; |
| 76 | else StmtVisitor<StmtPrinter>::Visit(S); |
| 77 | } |
| 78 | |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 79 | void VisitStmt(Stmt *Node); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 80 | #define STMT(N, CLASS, PARENT) \ |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 81 | void Visit##CLASS(CLASS *Node); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 82 | #include "clang/AST/StmtNodes.def" |
| 83 | }; |
| 84 | } |
| 85 | |
| 86 | //===----------------------------------------------------------------------===// |
| 87 | // Stmt printing methods. |
| 88 | //===----------------------------------------------------------------------===// |
| 89 | |
| 90 | void StmtPrinter::VisitStmt(Stmt *Node) { |
| 91 | Indent() << "<<unknown stmt type>>\n"; |
| 92 | } |
| 93 | |
| 94 | /// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and |
| 95 | /// with no newline after the }. |
| 96 | void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) { |
| 97 | OS << "{\n"; |
| 98 | for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end(); |
| 99 | I != E; ++I) |
| 100 | PrintStmt(*I); |
| 101 | |
| 102 | Indent() << "}"; |
| 103 | } |
| 104 | |
| 105 | void StmtPrinter::PrintRawDecl(Decl *D) { |
| 106 | // FIXME: Need to complete/beautify this... this code simply shows the |
| 107 | // nodes are where they need to be. |
| 108 | if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) { |
| 109 | OS << "typedef " << localType->getUnderlyingType().getAsString(); |
| 110 | OS << " " << localType->getName(); |
| 111 | } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 112 | // Emit storage class for vardecls. |
| 113 | if (VarDecl *V = dyn_cast<VarDecl>(VD)) { |
| 114 | switch (V->getStorageClass()) { |
| 115 | default: assert(0 && "Unknown storage class!"); |
| 116 | case VarDecl::None: break; |
| 117 | case VarDecl::Extern: OS << "extern "; break; |
| 118 | case VarDecl::Static: OS << "static "; break; |
| 119 | case VarDecl::Auto: OS << "auto "; break; |
| 120 | case VarDecl::Register: OS << "register "; break; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | std::string Name = VD->getName(); |
| 125 | VD->getType().getAsStringInternal(Name); |
| 126 | OS << Name; |
| 127 | |
Chris Lattner | 24c3990 | 2007-07-12 00:36:32 +0000 | [diff] [blame] | 128 | // If this is a vardecl with an initializer, emit it. |
| 129 | if (VarDecl *V = dyn_cast<VarDecl>(VD)) { |
| 130 | if (V->getInit()) { |
| 131 | OS << " = "; |
| 132 | PrintExpr(V->getInit()); |
| 133 | } |
| 134 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 135 | } else { |
| 136 | // FIXME: "struct x;" |
| 137 | assert(0 && "Unexpected decl"); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | |
| 142 | void StmtPrinter::VisitNullStmt(NullStmt *Node) { |
| 143 | Indent() << ";\n"; |
| 144 | } |
| 145 | |
| 146 | void StmtPrinter::VisitDeclStmt(DeclStmt *Node) { |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 147 | for (ScopedDecl *D = Node->getDecl(); D; D = D->getNextDeclarator()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 148 | Indent(); |
| 149 | PrintRawDecl(D); |
| 150 | OS << ";\n"; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) { |
| 155 | Indent(); |
| 156 | PrintRawCompoundStmt(Node); |
| 157 | OS << "\n"; |
| 158 | } |
| 159 | |
| 160 | void StmtPrinter::VisitCaseStmt(CaseStmt *Node) { |
| 161 | Indent(-1) << "case "; |
| 162 | PrintExpr(Node->getLHS()); |
| 163 | if (Node->getRHS()) { |
| 164 | OS << " ... "; |
| 165 | PrintExpr(Node->getRHS()); |
| 166 | } |
| 167 | OS << ":\n"; |
| 168 | |
| 169 | PrintStmt(Node->getSubStmt(), 0); |
| 170 | } |
| 171 | |
| 172 | void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) { |
| 173 | Indent(-1) << "default:\n"; |
| 174 | PrintStmt(Node->getSubStmt(), 0); |
| 175 | } |
| 176 | |
| 177 | void StmtPrinter::VisitLabelStmt(LabelStmt *Node) { |
| 178 | Indent(-1) << Node->getName() << ":\n"; |
| 179 | PrintStmt(Node->getSubStmt(), 0); |
| 180 | } |
| 181 | |
| 182 | void StmtPrinter::PrintRawIfStmt(IfStmt *If) { |
| 183 | OS << "if "; |
| 184 | PrintExpr(If->getCond()); |
| 185 | |
| 186 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) { |
| 187 | OS << ' '; |
| 188 | PrintRawCompoundStmt(CS); |
| 189 | OS << (If->getElse() ? ' ' : '\n'); |
| 190 | } else { |
| 191 | OS << '\n'; |
| 192 | PrintStmt(If->getThen()); |
| 193 | if (If->getElse()) Indent(); |
| 194 | } |
| 195 | |
| 196 | if (Stmt *Else = If->getElse()) { |
| 197 | OS << "else"; |
| 198 | |
| 199 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) { |
| 200 | OS << ' '; |
| 201 | PrintRawCompoundStmt(CS); |
| 202 | OS << '\n'; |
| 203 | } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) { |
| 204 | OS << ' '; |
| 205 | PrintRawIfStmt(ElseIf); |
| 206 | } else { |
| 207 | OS << '\n'; |
| 208 | PrintStmt(If->getElse()); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | void StmtPrinter::VisitIfStmt(IfStmt *If) { |
| 214 | Indent(); |
| 215 | PrintRawIfStmt(If); |
| 216 | } |
| 217 | |
| 218 | void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) { |
| 219 | Indent() << "switch ("; |
| 220 | PrintExpr(Node->getCond()); |
| 221 | OS << ")"; |
| 222 | |
| 223 | // Pretty print compoundstmt bodies (very common). |
| 224 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { |
| 225 | OS << " "; |
| 226 | PrintRawCompoundStmt(CS); |
| 227 | OS << "\n"; |
| 228 | } else { |
| 229 | OS << "\n"; |
| 230 | PrintStmt(Node->getBody()); |
| 231 | } |
| 232 | } |
| 233 | |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 234 | void StmtPrinter::VisitSwitchCase(SwitchCase*) { |
| 235 | assert(0 && "SwitchCase is an abstract class"); |
| 236 | } |
| 237 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 238 | void StmtPrinter::VisitWhileStmt(WhileStmt *Node) { |
| 239 | Indent() << "while ("; |
| 240 | PrintExpr(Node->getCond()); |
| 241 | OS << ")\n"; |
| 242 | PrintStmt(Node->getBody()); |
| 243 | } |
| 244 | |
| 245 | void StmtPrinter::VisitDoStmt(DoStmt *Node) { |
Chris Lattner | 8bdcc47 | 2007-09-15 21:49:37 +0000 | [diff] [blame] | 246 | Indent() << "do "; |
| 247 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { |
| 248 | PrintRawCompoundStmt(CS); |
| 249 | OS << " "; |
| 250 | } else { |
| 251 | OS << "\n"; |
| 252 | PrintStmt(Node->getBody()); |
| 253 | Indent(); |
| 254 | } |
| 255 | |
| 256 | OS << "while "; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 257 | PrintExpr(Node->getCond()); |
| 258 | OS << ";\n"; |
| 259 | } |
| 260 | |
| 261 | void StmtPrinter::VisitForStmt(ForStmt *Node) { |
| 262 | Indent() << "for ("; |
| 263 | if (Node->getInit()) { |
| 264 | if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit())) |
| 265 | PrintRawDecl(DS->getDecl()); |
| 266 | else |
| 267 | PrintExpr(cast<Expr>(Node->getInit())); |
| 268 | } |
Chris Lattner | 8bdcc47 | 2007-09-15 21:49:37 +0000 | [diff] [blame] | 269 | OS << ";"; |
| 270 | if (Node->getCond()) { |
| 271 | OS << " "; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 272 | PrintExpr(Node->getCond()); |
Chris Lattner | 8bdcc47 | 2007-09-15 21:49:37 +0000 | [diff] [blame] | 273 | } |
| 274 | OS << ";"; |
| 275 | if (Node->getInc()) { |
| 276 | OS << " "; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 277 | PrintExpr(Node->getInc()); |
Chris Lattner | 8bdcc47 | 2007-09-15 21:49:37 +0000 | [diff] [blame] | 278 | } |
| 279 | OS << ") "; |
| 280 | |
| 281 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { |
| 282 | PrintRawCompoundStmt(CS); |
| 283 | OS << "\n"; |
| 284 | } else { |
| 285 | OS << "\n"; |
| 286 | PrintStmt(Node->getBody()); |
| 287 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void StmtPrinter::VisitGotoStmt(GotoStmt *Node) { |
| 291 | Indent() << "goto " << Node->getLabel()->getName() << ";\n"; |
| 292 | } |
| 293 | |
| 294 | void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) { |
| 295 | Indent() << "goto *"; |
| 296 | PrintExpr(Node->getTarget()); |
| 297 | OS << ";\n"; |
| 298 | } |
| 299 | |
| 300 | void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) { |
| 301 | Indent() << "continue;\n"; |
| 302 | } |
| 303 | |
| 304 | void StmtPrinter::VisitBreakStmt(BreakStmt *Node) { |
| 305 | Indent() << "break;\n"; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) { |
| 310 | Indent() << "return"; |
| 311 | if (Node->getRetValue()) { |
| 312 | OS << " "; |
| 313 | PrintExpr(Node->getRetValue()); |
| 314 | } |
| 315 | OS << ";\n"; |
| 316 | } |
| 317 | |
| 318 | //===----------------------------------------------------------------------===// |
| 319 | // Expr printing methods. |
| 320 | //===----------------------------------------------------------------------===// |
| 321 | |
| 322 | void StmtPrinter::VisitExpr(Expr *Node) { |
| 323 | OS << "<<unknown expr type>>"; |
| 324 | } |
| 325 | |
| 326 | void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) { |
| 327 | OS << Node->getDecl()->getName(); |
| 328 | } |
| 329 | |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 330 | void StmtPrinter::VisitPreDefinedExpr(PreDefinedExpr *Node) { |
| 331 | switch (Node->getIdentType()) { |
| 332 | default: |
| 333 | assert(0 && "unknown case"); |
| 334 | case PreDefinedExpr::Func: |
| 335 | OS << "__func__"; |
| 336 | break; |
| 337 | case PreDefinedExpr::Function: |
| 338 | OS << "__FUNCTION__"; |
| 339 | break; |
| 340 | case PreDefinedExpr::PrettyFunction: |
| 341 | OS << "__PRETTY_FUNCTION__"; |
| 342 | break; |
| 343 | } |
| 344 | } |
| 345 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 346 | void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) { |
Chris Lattner | 8bf9f07 | 2007-07-13 23:58:20 +0000 | [diff] [blame] | 347 | // FIXME should print an L for wchar_t constants |
Chris Lattner | b0a721a | 2007-07-13 05:18:11 +0000 | [diff] [blame] | 348 | unsigned value = Node->getValue(); |
Chris Lattner | 8bf9f07 | 2007-07-13 23:58:20 +0000 | [diff] [blame] | 349 | switch (value) { |
| 350 | case '\\': |
| 351 | OS << "'\\\\'"; |
| 352 | break; |
| 353 | case '\'': |
| 354 | OS << "'\\''"; |
| 355 | break; |
| 356 | case '\a': |
| 357 | // TODO: K&R: the meaning of '\\a' is different in traditional C |
| 358 | OS << "'\\a'"; |
| 359 | break; |
| 360 | case '\b': |
| 361 | OS << "'\\b'"; |
| 362 | break; |
| 363 | // Nonstandard escape sequence. |
| 364 | /*case '\e': |
| 365 | OS << "'\\e'"; |
| 366 | break;*/ |
| 367 | case '\f': |
| 368 | OS << "'\\f'"; |
| 369 | break; |
| 370 | case '\n': |
| 371 | OS << "'\\n'"; |
| 372 | break; |
| 373 | case '\r': |
| 374 | OS << "'\\r'"; |
| 375 | break; |
| 376 | case '\t': |
| 377 | OS << "'\\t'"; |
| 378 | break; |
| 379 | case '\v': |
| 380 | OS << "'\\v'"; |
| 381 | break; |
| 382 | default: |
| 383 | if (isprint(value) && value < 256) { |
| 384 | OS << "'" << (char)value << "'"; |
| 385 | } else if (value < 256) { |
| 386 | OS << "'\\x" << std::hex << value << std::dec << "'"; |
| 387 | } else { |
| 388 | // FIXME what to really do here? |
| 389 | OS << value; |
| 390 | } |
Chris Lattner | b0a721a | 2007-07-13 05:18:11 +0000 | [diff] [blame] | 391 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) { |
| 395 | bool isSigned = Node->getType()->isSignedIntegerType(); |
| 396 | OS << Node->getValue().toString(10, isSigned); |
| 397 | |
| 398 | // Emit suffixes. Integer literals are always a builtin integer type. |
| 399 | switch (cast<BuiltinType>(Node->getType().getCanonicalType())->getKind()) { |
| 400 | default: assert(0 && "Unexpected type for integer literal!"); |
| 401 | case BuiltinType::Int: break; // no suffix. |
| 402 | case BuiltinType::UInt: OS << 'U'; break; |
| 403 | case BuiltinType::Long: OS << 'L'; break; |
| 404 | case BuiltinType::ULong: OS << "UL"; break; |
| 405 | case BuiltinType::LongLong: OS << "LL"; break; |
| 406 | case BuiltinType::ULongLong: OS << "ULL"; break; |
| 407 | } |
| 408 | } |
| 409 | void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) { |
Chris Lattner | 86e499d | 2007-08-01 00:23:58 +0000 | [diff] [blame] | 410 | // FIXME: print value more precisely. |
Chris Lattner | c9bec4b | 2007-09-22 18:47:25 +0000 | [diff] [blame] | 411 | OS << Node->getValueAsDouble(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 412 | } |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 413 | |
| 414 | void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) { |
| 415 | PrintExpr(Node->getSubExpr()); |
| 416 | OS << "i"; |
| 417 | } |
| 418 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 419 | void StmtPrinter::VisitStringLiteral(StringLiteral *Str) { |
| 420 | if (Str->isWide()) OS << 'L'; |
| 421 | OS << '"'; |
Anders Carlsson | ee98ac5 | 2007-10-15 02:50:23 +0000 | [diff] [blame] | 422 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 423 | // FIXME: this doesn't print wstrings right. |
| 424 | for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) { |
| 425 | switch (Str->getStrData()[i]) { |
| 426 | default: OS << Str->getStrData()[i]; break; |
| 427 | // Handle some common ones to make dumps prettier. |
| 428 | case '\\': OS << "\\\\"; break; |
| 429 | case '"': OS << "\\\""; break; |
| 430 | case '\n': OS << "\\n"; break; |
| 431 | case '\t': OS << "\\t"; break; |
| 432 | case '\a': OS << "\\a"; break; |
| 433 | case '\b': OS << "\\b"; break; |
| 434 | } |
| 435 | } |
| 436 | OS << '"'; |
| 437 | } |
| 438 | void StmtPrinter::VisitParenExpr(ParenExpr *Node) { |
| 439 | OS << "("; |
| 440 | PrintExpr(Node->getSubExpr()); |
| 441 | OS << ")"; |
| 442 | } |
| 443 | void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) { |
Chris Lattner | 296bf19 | 2007-08-23 21:46:40 +0000 | [diff] [blame] | 444 | if (!Node->isPostfix()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 445 | OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); |
Chris Lattner | 296bf19 | 2007-08-23 21:46:40 +0000 | [diff] [blame] | 446 | |
| 447 | // Print a space if this is an "identifier operator" like sizeof or __real. |
| 448 | switch (Node->getOpcode()) { |
| 449 | default: break; |
| 450 | case UnaryOperator::SizeOf: |
| 451 | case UnaryOperator::AlignOf: |
| 452 | case UnaryOperator::Real: |
| 453 | case UnaryOperator::Imag: |
| 454 | case UnaryOperator::Extension: |
| 455 | OS << ' '; |
| 456 | break; |
| 457 | } |
| 458 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 459 | PrintExpr(Node->getSubExpr()); |
| 460 | |
| 461 | if (Node->isPostfix()) |
| 462 | OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 463 | } |
Chris Lattner | 704fe35 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 464 | |
| 465 | bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) { |
| 466 | if (isa<CompoundLiteralExpr>(E)) { |
| 467 | // Base case, print the type and comma. |
| 468 | OS << E->getType().getAsString() << ", "; |
| 469 | return true; |
| 470 | } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) { |
| 471 | PrintOffsetOfDesignator(ASE->getLHS()); |
| 472 | OS << "["; |
| 473 | PrintExpr(ASE->getRHS()); |
| 474 | OS << "]"; |
| 475 | return false; |
| 476 | } else { |
| 477 | MemberExpr *ME = cast<MemberExpr>(E); |
| 478 | bool IsFirst = PrintOffsetOfDesignator(ME->getBase()); |
| 479 | OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getName(); |
| 480 | return false; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) { |
| 485 | OS << "__builtin_offsetof("; |
| 486 | PrintOffsetOfDesignator(Node->getSubExpr()); |
| 487 | OS << ")"; |
| 488 | } |
| 489 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 490 | void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) { |
| 491 | OS << (Node->isSizeOf() ? "sizeof(" : "__alignof("); |
| 492 | OS << Node->getArgumentType().getAsString() << ")"; |
| 493 | } |
| 494 | void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) { |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 495 | PrintExpr(Node->getLHS()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 496 | OS << "["; |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 497 | PrintExpr(Node->getRHS()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 498 | OS << "]"; |
| 499 | } |
| 500 | |
| 501 | void StmtPrinter::VisitCallExpr(CallExpr *Call) { |
| 502 | PrintExpr(Call->getCallee()); |
| 503 | OS << "("; |
| 504 | for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) { |
| 505 | if (i) OS << ", "; |
| 506 | PrintExpr(Call->getArg(i)); |
| 507 | } |
| 508 | OS << ")"; |
| 509 | } |
| 510 | void StmtPrinter::VisitMemberExpr(MemberExpr *Node) { |
| 511 | PrintExpr(Node->getBase()); |
| 512 | OS << (Node->isArrow() ? "->" : "."); |
| 513 | |
| 514 | FieldDecl *Field = Node->getMemberDecl(); |
| 515 | assert(Field && "MemberExpr should alway reference a field!"); |
| 516 | OS << Field->getName(); |
| 517 | } |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 518 | void StmtPrinter::VisitOCUVectorElementExpr(OCUVectorElementExpr *Node) { |
Steve Naroff | 31a4584 | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 519 | PrintExpr(Node->getBase()); |
| 520 | OS << "."; |
| 521 | OS << Node->getAccessor().getName(); |
| 522 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 523 | void StmtPrinter::VisitCastExpr(CastExpr *Node) { |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 524 | OS << "(" << Node->getType().getAsString() << ")"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 525 | PrintExpr(Node->getSubExpr()); |
| 526 | } |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 527 | void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) { |
| 528 | OS << "(" << Node->getType().getAsString() << ")"; |
| 529 | PrintExpr(Node->getInitializer()); |
| 530 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 531 | void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) { |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 532 | // No need to print anything, simply forward to the sub expression. |
| 533 | PrintExpr(Node->getSubExpr()); |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 534 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 535 | void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) { |
| 536 | PrintExpr(Node->getLHS()); |
| 537 | OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; |
| 538 | PrintExpr(Node->getRHS()); |
| 539 | } |
Chris Lattner | eb14fe8 | 2007-08-25 02:00:02 +0000 | [diff] [blame] | 540 | void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) { |
| 541 | PrintExpr(Node->getLHS()); |
| 542 | OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; |
| 543 | PrintExpr(Node->getRHS()); |
| 544 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 545 | void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { |
| 546 | PrintExpr(Node->getCond()); |
| 547 | OS << " ? "; |
| 548 | PrintExpr(Node->getLHS()); |
| 549 | OS << " : "; |
| 550 | PrintExpr(Node->getRHS()); |
| 551 | } |
| 552 | |
| 553 | // GNU extensions. |
| 554 | |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 555 | void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 556 | OS << "&&" << Node->getLabel()->getName(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 559 | void StmtPrinter::VisitStmtExpr(StmtExpr *E) { |
| 560 | OS << "("; |
| 561 | PrintRawCompoundStmt(E->getSubStmt()); |
| 562 | OS << ")"; |
| 563 | } |
| 564 | |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 565 | void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) { |
| 566 | OS << "__builtin_types_compatible_p("; |
| 567 | OS << Node->getArgType1().getAsString() << ","; |
| 568 | OS << Node->getArgType2().getAsString() << ")"; |
| 569 | } |
| 570 | |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 571 | void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) { |
| 572 | OS << "__builtin_choose_expr("; |
| 573 | PrintExpr(Node->getCond()); |
Chris Lattner | 94f05e3 | 2007-08-04 00:20:15 +0000 | [diff] [blame] | 574 | OS << ", "; |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 575 | PrintExpr(Node->getLHS()); |
Chris Lattner | 94f05e3 | 2007-08-04 00:20:15 +0000 | [diff] [blame] | 576 | OS << ", "; |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 577 | PrintExpr(Node->getRHS()); |
| 578 | OS << ")"; |
| 579 | } |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 580 | |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 581 | void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { |
| 582 | OS << "{ "; |
| 583 | for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { |
| 584 | if (i) OS << ", "; |
| 585 | PrintExpr(Node->getInit(i)); |
| 586 | } |
| 587 | OS << " }"; |
| 588 | } |
| 589 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame^] | 590 | void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) { |
| 591 | OS << "va_arg("; |
| 592 | PrintExpr(Node->getSubExpr()); |
| 593 | OS << ", "; |
| 594 | OS << Node->getType().getAsString(); |
| 595 | OS << ")"; |
| 596 | } |
| 597 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 598 | // C++ |
| 599 | |
| 600 | void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) { |
Chris Lattner | 36460ee | 2007-08-09 17:34:19 +0000 | [diff] [blame] | 601 | OS << CXXCastExpr::getOpcodeStr(Node->getOpcode()) << '<'; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 602 | OS << Node->getDestType().getAsString() << ">("; |
| 603 | PrintExpr(Node->getSubExpr()); |
| 604 | OS << ")"; |
| 605 | } |
| 606 | |
| 607 | void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { |
| 608 | OS << (Node->getValue() ? "true" : "false"); |
| 609 | } |
| 610 | |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 611 | // Obj-C |
| 612 | |
| 613 | void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { |
| 614 | OS << "@"; |
| 615 | VisitStringLiteral(Node->getString()); |
| 616 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 617 | |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 618 | void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { |
| 619 | OS << "@encode("; |
| 620 | OS << Node->getEncodedType().getAsString() << ")"; |
| 621 | } |
| 622 | |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 623 | void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) { |
| 624 | OS << "["; |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 625 | Expr *receiver = Mess->getReceiver(); |
| 626 | if (receiver) PrintExpr(receiver); |
| 627 | else OS << Mess->getClassName()->getName(); |
| 628 | Selector &selector = Mess->getSelector(); |
| 629 | if (selector.isUnarySelector()) { |
| 630 | OS << " " << selector.getIdentifierInfoForSlot(0)->getName(); |
| 631 | } else { |
| 632 | for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) { |
| 633 | OS << " " << selector.getIdentifierInfoForSlot(i)->getName() << ":"; |
| 634 | PrintExpr(Mess->getArg(i)); |
| 635 | } |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 636 | } |
| 637 | OS << "]"; |
| 638 | } |
| 639 | |
| 640 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 641 | //===----------------------------------------------------------------------===// |
| 642 | // Stmt method implementations |
| 643 | //===----------------------------------------------------------------------===// |
| 644 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 645 | void Stmt::dumpPretty() const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 646 | // FIXME: eliminate use of <iostream> |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 647 | printPretty(std::cerr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 650 | void Stmt::printPretty(std::ostream &OS, PrinterHelper* Helper) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 651 | if (this == 0) { |
| 652 | OS << "<NULL>"; |
| 653 | return; |
| 654 | } |
| 655 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 656 | StmtPrinter P(OS, Helper); |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 657 | P.Visit(const_cast<Stmt*>(this)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 658 | } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 659 | |
| 660 | //===----------------------------------------------------------------------===// |
| 661 | // PrinterHelper |
| 662 | //===----------------------------------------------------------------------===// |
| 663 | |
| 664 | // Implement virtual destructor. |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 665 | PrinterHelper::~PrinterHelper() {} |