Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 9557878 | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 08176a5 | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 18 | #include "clang/AST/PrettyPrinter.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 19 | #include "clang/Lex/IdentifierTable.h" |
| 20 | #include "llvm/Support/Compiler.h" |
| 21 | #include <iostream> |
| 22 | #include <iomanip> |
| 23 | using namespace clang; |
| 24 | |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | // StmtPrinter Visitor |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | namespace { |
Chris Lattner | 7ba21fa | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 30 | class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 31 | std::ostream &OS; |
| 32 | unsigned IndentLevel; |
Ted Kremenek | 08176a5 | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 33 | clang::PrinterHelper* Helper; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 34 | public: |
Ted Kremenek | 08176a5 | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 35 | StmtPrinter(std::ostream &os, PrinterHelper* helper) : |
| 36 | OS(os), IndentLevel(0), Helper(helper) {} |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 7ba21fa | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 43 | Visit(S); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 44 | OS << ";\n"; |
| 45 | } else if (S) { |
Chris Lattner | 7ba21fa | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 46 | Visit(S); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 7ba21fa | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 59 | Visit(E); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 2af6a80 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 70 | bool PrintOffsetOfDesignator(Expr *E); |
| 71 | void VisitUnaryOffsetOf(UnaryOperator *Node); |
| 72 | |
Ted Kremenek | 08176a5 | 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 | 7ba21fa | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 79 | void VisitStmt(Stmt *Node); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 80 | #define STMT(N, CLASS, PARENT) \ |
Chris Lattner | 7ba21fa | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 81 | void Visit##CLASS(CLASS *Node); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | |
| 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 | } |
| 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) { |
| 147 | for (Decl *D = Node->getDecl(); D; D = D->getNextDeclarator()) { |
| 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 | |
| 234 | void StmtPrinter::VisitSwitchCase(SwitchCase*) { |
| 235 | assert(0 && "SwitchCase is an abstract class"); |
| 236 | } |
| 237 | |
| 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) { |
| 246 | Indent() << "do\n"; |
| 247 | PrintStmt(Node->getBody()); |
| 248 | Indent() << "while "; |
| 249 | PrintExpr(Node->getCond()); |
| 250 | OS << ";\n"; |
| 251 | } |
| 252 | |
| 253 | void StmtPrinter::VisitForStmt(ForStmt *Node) { |
| 254 | Indent() << "for ("; |
| 255 | if (Node->getInit()) { |
| 256 | if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit())) |
| 257 | PrintRawDecl(DS->getDecl()); |
| 258 | else |
| 259 | PrintExpr(cast<Expr>(Node->getInit())); |
| 260 | } |
| 261 | OS << "; "; |
| 262 | if (Node->getCond()) |
| 263 | PrintExpr(Node->getCond()); |
| 264 | OS << "; "; |
| 265 | if (Node->getInc()) |
| 266 | PrintExpr(Node->getInc()); |
| 267 | OS << ")\n"; |
| 268 | PrintStmt(Node->getBody()); |
| 269 | } |
| 270 | |
| 271 | void StmtPrinter::VisitGotoStmt(GotoStmt *Node) { |
| 272 | Indent() << "goto " << Node->getLabel()->getName() << ";\n"; |
| 273 | } |
| 274 | |
| 275 | void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) { |
| 276 | Indent() << "goto *"; |
| 277 | PrintExpr(Node->getTarget()); |
| 278 | OS << ";\n"; |
| 279 | } |
| 280 | |
| 281 | void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) { |
| 282 | Indent() << "continue;\n"; |
| 283 | } |
| 284 | |
| 285 | void StmtPrinter::VisitBreakStmt(BreakStmt *Node) { |
| 286 | Indent() << "break;\n"; |
| 287 | } |
| 288 | |
| 289 | |
| 290 | void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) { |
| 291 | Indent() << "return"; |
| 292 | if (Node->getRetValue()) { |
| 293 | OS << " "; |
| 294 | PrintExpr(Node->getRetValue()); |
| 295 | } |
| 296 | OS << ";\n"; |
| 297 | } |
| 298 | |
| 299 | //===----------------------------------------------------------------------===// |
| 300 | // Expr printing methods. |
| 301 | //===----------------------------------------------------------------------===// |
| 302 | |
| 303 | void StmtPrinter::VisitExpr(Expr *Node) { |
| 304 | OS << "<<unknown expr type>>"; |
| 305 | } |
| 306 | |
| 307 | void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) { |
| 308 | OS << Node->getDecl()->getName(); |
| 309 | } |
| 310 | |
| 311 | void StmtPrinter::VisitPreDefinedExpr(PreDefinedExpr *Node) { |
| 312 | switch (Node->getIdentType()) { |
| 313 | default: |
| 314 | assert(0 && "unknown case"); |
| 315 | case PreDefinedExpr::Func: |
| 316 | OS << "__func__"; |
| 317 | break; |
| 318 | case PreDefinedExpr::Function: |
| 319 | OS << "__FUNCTION__"; |
| 320 | break; |
| 321 | case PreDefinedExpr::PrettyFunction: |
| 322 | OS << "__PRETTY_FUNCTION__"; |
| 323 | break; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) { |
| 328 | // FIXME should print an L for wchar_t constants |
| 329 | unsigned value = Node->getValue(); |
| 330 | switch (value) { |
| 331 | case '\\': |
| 332 | OS << "'\\\\'"; |
| 333 | break; |
| 334 | case '\'': |
| 335 | OS << "'\\''"; |
| 336 | break; |
| 337 | case '\a': |
| 338 | // TODO: K&R: the meaning of '\\a' is different in traditional C |
| 339 | OS << "'\\a'"; |
| 340 | break; |
| 341 | case '\b': |
| 342 | OS << "'\\b'"; |
| 343 | break; |
| 344 | // Nonstandard escape sequence. |
| 345 | /*case '\e': |
| 346 | OS << "'\\e'"; |
| 347 | break;*/ |
| 348 | case '\f': |
| 349 | OS << "'\\f'"; |
| 350 | break; |
| 351 | case '\n': |
| 352 | OS << "'\\n'"; |
| 353 | break; |
| 354 | case '\r': |
| 355 | OS << "'\\r'"; |
| 356 | break; |
| 357 | case '\t': |
| 358 | OS << "'\\t'"; |
| 359 | break; |
| 360 | case '\v': |
| 361 | OS << "'\\v'"; |
| 362 | break; |
| 363 | default: |
| 364 | if (isprint(value) && value < 256) { |
| 365 | OS << "'" << (char)value << "'"; |
| 366 | } else if (value < 256) { |
| 367 | OS << "'\\x" << std::hex << value << std::dec << "'"; |
| 368 | } else { |
| 369 | // FIXME what to really do here? |
| 370 | OS << value; |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) { |
| 376 | bool isSigned = Node->getType()->isSignedIntegerType(); |
| 377 | OS << Node->getValue().toString(10, isSigned); |
| 378 | |
| 379 | // Emit suffixes. Integer literals are always a builtin integer type. |
| 380 | switch (cast<BuiltinType>(Node->getType().getCanonicalType())->getKind()) { |
| 381 | default: assert(0 && "Unexpected type for integer literal!"); |
| 382 | case BuiltinType::Int: break; // no suffix. |
| 383 | case BuiltinType::UInt: OS << 'U'; break; |
| 384 | case BuiltinType::Long: OS << 'L'; break; |
| 385 | case BuiltinType::ULong: OS << "UL"; break; |
| 386 | case BuiltinType::LongLong: OS << "LL"; break; |
| 387 | case BuiltinType::ULongLong: OS << "ULL"; break; |
| 388 | } |
| 389 | } |
| 390 | void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) { |
Chris Lattner | 6a39040 | 2007-08-01 00:23:58 +0000 | [diff] [blame] | 391 | // FIXME: print value more precisely. |
| 392 | OS << Node->getValue(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 393 | } |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 394 | |
| 395 | void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) { |
| 396 | PrintExpr(Node->getSubExpr()); |
| 397 | OS << "i"; |
| 398 | } |
| 399 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 400 | void StmtPrinter::VisitStringLiteral(StringLiteral *Str) { |
| 401 | if (Str->isWide()) OS << 'L'; |
| 402 | OS << '"'; |
| 403 | |
| 404 | // FIXME: this doesn't print wstrings right. |
| 405 | for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) { |
| 406 | switch (Str->getStrData()[i]) { |
| 407 | default: OS << Str->getStrData()[i]; break; |
| 408 | // Handle some common ones to make dumps prettier. |
| 409 | case '\\': OS << "\\\\"; break; |
| 410 | case '"': OS << "\\\""; break; |
| 411 | case '\n': OS << "\\n"; break; |
| 412 | case '\t': OS << "\\t"; break; |
| 413 | case '\a': OS << "\\a"; break; |
| 414 | case '\b': OS << "\\b"; break; |
| 415 | } |
| 416 | } |
| 417 | OS << '"'; |
| 418 | } |
| 419 | void StmtPrinter::VisitParenExpr(ParenExpr *Node) { |
| 420 | OS << "("; |
| 421 | PrintExpr(Node->getSubExpr()); |
| 422 | OS << ")"; |
| 423 | } |
| 424 | void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) { |
Chris Lattner | 2b97b09 | 2007-08-23 21:46:40 +0000 | [diff] [blame] | 425 | if (!Node->isPostfix()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 426 | OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); |
Chris Lattner | 2b97b09 | 2007-08-23 21:46:40 +0000 | [diff] [blame] | 427 | |
| 428 | // Print a space if this is an "identifier operator" like sizeof or __real. |
| 429 | switch (Node->getOpcode()) { |
| 430 | default: break; |
| 431 | case UnaryOperator::SizeOf: |
| 432 | case UnaryOperator::AlignOf: |
| 433 | case UnaryOperator::Real: |
| 434 | case UnaryOperator::Imag: |
| 435 | case UnaryOperator::Extension: |
| 436 | OS << ' '; |
| 437 | break; |
| 438 | } |
| 439 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 440 | PrintExpr(Node->getSubExpr()); |
| 441 | |
| 442 | if (Node->isPostfix()) |
| 443 | OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 444 | } |
Chris Lattner | 2af6a80 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 445 | |
| 446 | bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) { |
| 447 | if (isa<CompoundLiteralExpr>(E)) { |
| 448 | // Base case, print the type and comma. |
| 449 | OS << E->getType().getAsString() << ", "; |
| 450 | return true; |
| 451 | } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) { |
| 452 | PrintOffsetOfDesignator(ASE->getLHS()); |
| 453 | OS << "["; |
| 454 | PrintExpr(ASE->getRHS()); |
| 455 | OS << "]"; |
| 456 | return false; |
| 457 | } else { |
| 458 | MemberExpr *ME = cast<MemberExpr>(E); |
| 459 | bool IsFirst = PrintOffsetOfDesignator(ME->getBase()); |
| 460 | OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getName(); |
| 461 | return false; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) { |
| 466 | OS << "__builtin_offsetof("; |
| 467 | PrintOffsetOfDesignator(Node->getSubExpr()); |
| 468 | OS << ")"; |
| 469 | } |
| 470 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 471 | void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) { |
| 472 | OS << (Node->isSizeOf() ? "sizeof(" : "__alignof("); |
| 473 | OS << Node->getArgumentType().getAsString() << ")"; |
| 474 | } |
| 475 | void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) { |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 476 | PrintExpr(Node->getLHS()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 477 | OS << "["; |
Ted Kremenek | 1c1700f | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 478 | PrintExpr(Node->getRHS()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 479 | OS << "]"; |
| 480 | } |
| 481 | |
| 482 | void StmtPrinter::VisitCallExpr(CallExpr *Call) { |
| 483 | PrintExpr(Call->getCallee()); |
| 484 | OS << "("; |
| 485 | for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) { |
| 486 | if (i) OS << ", "; |
| 487 | PrintExpr(Call->getArg(i)); |
| 488 | } |
| 489 | OS << ")"; |
| 490 | } |
| 491 | void StmtPrinter::VisitMemberExpr(MemberExpr *Node) { |
| 492 | PrintExpr(Node->getBase()); |
| 493 | OS << (Node->isArrow() ? "->" : "."); |
| 494 | |
| 495 | FieldDecl *Field = Node->getMemberDecl(); |
| 496 | assert(Field && "MemberExpr should alway reference a field!"); |
| 497 | OS << Field->getName(); |
| 498 | } |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 499 | void StmtPrinter::VisitOCUVectorElementExpr(OCUVectorElementExpr *Node) { |
Steve Naroff | c11705f | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 500 | PrintExpr(Node->getBase()); |
| 501 | OS << "."; |
| 502 | OS << Node->getAccessor().getName(); |
| 503 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 504 | void StmtPrinter::VisitCastExpr(CastExpr *Node) { |
| 505 | OS << "(" << Node->getType().getAsString() << ")"; |
| 506 | PrintExpr(Node->getSubExpr()); |
| 507 | } |
| 508 | void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) { |
| 509 | OS << "(" << Node->getType().getAsString() << ")"; |
| 510 | PrintExpr(Node->getInitializer()); |
| 511 | } |
| 512 | void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) { |
| 513 | // No need to print anything, simply forward to the sub expression. |
| 514 | PrintExpr(Node->getSubExpr()); |
| 515 | } |
| 516 | void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) { |
| 517 | PrintExpr(Node->getLHS()); |
| 518 | OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; |
| 519 | PrintExpr(Node->getRHS()); |
| 520 | } |
Chris Lattner | 06078d2 | 2007-08-25 02:00:02 +0000 | [diff] [blame] | 521 | void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) { |
| 522 | PrintExpr(Node->getLHS()); |
| 523 | OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; |
| 524 | PrintExpr(Node->getRHS()); |
| 525 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 526 | void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { |
| 527 | PrintExpr(Node->getCond()); |
| 528 | OS << " ? "; |
| 529 | PrintExpr(Node->getLHS()); |
| 530 | OS << " : "; |
| 531 | PrintExpr(Node->getRHS()); |
| 532 | } |
| 533 | |
| 534 | // GNU extensions. |
| 535 | |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 536 | void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 537 | OS << "&&" << Node->getLabel()->getName(); |
| 538 | } |
| 539 | |
| 540 | void StmtPrinter::VisitStmtExpr(StmtExpr *E) { |
| 541 | OS << "("; |
| 542 | PrintRawCompoundStmt(E->getSubStmt()); |
| 543 | OS << ")"; |
| 544 | } |
| 545 | |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 546 | void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) { |
| 547 | OS << "__builtin_types_compatible_p("; |
| 548 | OS << Node->getArgType1().getAsString() << ","; |
| 549 | OS << Node->getArgType2().getAsString() << ")"; |
| 550 | } |
| 551 | |
Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 552 | void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) { |
| 553 | OS << "__builtin_choose_expr("; |
| 554 | PrintExpr(Node->getCond()); |
Chris Lattner | 44fcf4f | 2007-08-04 00:20:15 +0000 | [diff] [blame] | 555 | OS << ", "; |
Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 556 | PrintExpr(Node->getLHS()); |
Chris Lattner | 44fcf4f | 2007-08-04 00:20:15 +0000 | [diff] [blame] | 557 | OS << ", "; |
Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 558 | PrintExpr(Node->getRHS()); |
| 559 | OS << ")"; |
| 560 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 561 | |
Anders Carlsson | 762b7c7 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 562 | void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { |
| 563 | OS << "{ "; |
| 564 | for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { |
| 565 | if (i) OS << ", "; |
| 566 | PrintExpr(Node->getInit(i)); |
| 567 | } |
| 568 | OS << " }"; |
| 569 | } |
| 570 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 571 | // C++ |
| 572 | |
| 573 | void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) { |
Chris Lattner | ac485c1 | 2007-08-09 17:34:19 +0000 | [diff] [blame] | 574 | OS << CXXCastExpr::getOpcodeStr(Node->getOpcode()) << '<'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 575 | OS << Node->getDestType().getAsString() << ">("; |
| 576 | PrintExpr(Node->getSubExpr()); |
| 577 | OS << ")"; |
| 578 | } |
| 579 | |
| 580 | void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { |
| 581 | OS << (Node->getValue() ? "true" : "false"); |
| 582 | } |
| 583 | |
Anders Carlsson | a66cad4 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 584 | // Obj-C |
| 585 | |
| 586 | void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { |
| 587 | OS << "@"; |
| 588 | VisitStringLiteral(Node->getString()); |
| 589 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 590 | |
Anders Carlsson | 8be1d40 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 591 | void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { |
| 592 | OS << "@encode("; |
| 593 | OS << Node->getEncodedType().getAsString() << ")"; |
| 594 | } |
| 595 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 596 | //===----------------------------------------------------------------------===// |
| 597 | // Stmt method implementations |
| 598 | //===----------------------------------------------------------------------===// |
| 599 | |
Chris Lattner | 9557878 | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 600 | void Stmt::dumpPretty() const { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 601 | // FIXME: eliminate use of <iostream> |
Chris Lattner | 9557878 | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 602 | printPretty(std::cerr); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Ted Kremenek | 08176a5 | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 605 | void Stmt::printPretty(std::ostream &OS, PrinterHelper* Helper) const { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 606 | if (this == 0) { |
| 607 | OS << "<NULL>"; |
| 608 | return; |
| 609 | } |
| 610 | |
Ted Kremenek | 08176a5 | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 611 | StmtPrinter P(OS, Helper); |
Chris Lattner | 7ba21fa | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 612 | P.Visit(const_cast<Stmt*>(this)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 613 | } |
Ted Kremenek | 08176a5 | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 614 | |
| 615 | //===----------------------------------------------------------------------===// |
| 616 | // PrinterHelper |
| 617 | //===----------------------------------------------------------------------===// |
| 618 | |
| 619 | // Implement virtual destructor. |
Gabor Greif | 61ce98c | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 620 | PrinterHelper::~PrinterHelper() {} |