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