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" |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclCXX.h" |
Ted Kremenek | 91d1d7a | 2007-10-17 18:36:42 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 18 | #include "clang/AST/PrettyPrinter.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Format.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // StmtPrinter Visitor |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
| 27 | namespace { |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 28 | class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> { |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 29 | llvm::raw_ostream &OS; |
Eli Friedman | 48d14a2 | 2009-05-30 05:03:24 +0000 | [diff] [blame] | 30 | ASTContext &Context; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | unsigned IndentLevel; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 32 | clang::PrinterHelper* Helper; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 33 | PrintingPolicy Policy; |
| 34 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | public: |
Eli Friedman | 48d14a2 | 2009-05-30 05:03:24 +0000 | [diff] [blame] | 36 | StmtPrinter(llvm::raw_ostream &os, ASTContext &C, PrinterHelper* helper, |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 37 | const PrintingPolicy &Policy, |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 38 | unsigned Indentation = 0) |
Eli Friedman | 48d14a2 | 2009-05-30 05:03:24 +0000 | [diff] [blame] | 39 | : OS(os), Context(C), IndentLevel(Indentation), Helper(helper), |
| 40 | Policy(Policy) {} |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 42 | void PrintStmt(Stmt *S) { |
| 43 | PrintStmt(S, Policy.Indentation); |
| 44 | } |
| 45 | |
| 46 | void PrintStmt(Stmt *S, int SubIndent) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | IndentLevel += SubIndent; |
| 48 | if (S && isa<Expr>(S)) { |
| 49 | // If this is an expr used in a stmt context, indent and newline it. |
| 50 | Indent(); |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 51 | Visit(S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | OS << ";\n"; |
| 53 | } else if (S) { |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 54 | Visit(S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 55 | } else { |
| 56 | Indent() << "<<<NULL STATEMENT>>>\n"; |
| 57 | } |
| 58 | IndentLevel -= SubIndent; |
| 59 | } |
Eli Friedman | db23b15 | 2009-05-30 00:19:54 +0000 | [diff] [blame] | 60 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 61 | void PrintRawCompoundStmt(CompoundStmt *S); |
| 62 | void PrintRawDecl(Decl *D); |
Ted Kremenek | ecd64c5 | 2008-10-06 18:39:36 +0000 | [diff] [blame] | 63 | void PrintRawDeclStmt(DeclStmt *S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 64 | void PrintRawIfStmt(IfStmt *If); |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 65 | void PrintRawCXXCatchStmt(CXXCatchStmt *Catch); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 66 | |
| 67 | void PrintExpr(Expr *E) { |
| 68 | if (E) |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 69 | Visit(E); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 70 | else |
| 71 | OS << "<null expr>"; |
| 72 | } |
| 73 | |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 74 | llvm::raw_ostream &Indent(int Delta = 0) { |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 75 | for (int i = 0, e = IndentLevel+Delta; i < e; ++i) |
| 76 | OS << " "; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 77 | return OS; |
| 78 | } |
| 79 | |
Chris Lattner | 704fe35 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 80 | bool PrintOffsetOfDesignator(Expr *E); |
| 81 | void VisitUnaryOffsetOf(UnaryOperator *Node); |
| 82 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 83 | void Visit(Stmt* S) { |
| 84 | if (Helper && Helper->handledStmt(S,OS)) |
| 85 | return; |
| 86 | else StmtVisitor<StmtPrinter>::Visit(S); |
| 87 | } |
| 88 | |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 89 | void VisitStmt(Stmt *Node); |
Douglas Gregor | f2cad86 | 2008-11-14 12:46:07 +0000 | [diff] [blame] | 90 | #define STMT(CLASS, PARENT) \ |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 91 | void Visit##CLASS(CLASS *Node); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 92 | #include "clang/AST/StmtNodes.def" |
| 93 | }; |
| 94 | } |
| 95 | |
| 96 | //===----------------------------------------------------------------------===// |
| 97 | // Stmt printing methods. |
| 98 | //===----------------------------------------------------------------------===// |
| 99 | |
| 100 | void StmtPrinter::VisitStmt(Stmt *Node) { |
| 101 | Indent() << "<<unknown stmt type>>\n"; |
| 102 | } |
| 103 | |
| 104 | /// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and |
| 105 | /// with no newline after the }. |
| 106 | void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) { |
| 107 | OS << "{\n"; |
| 108 | for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end(); |
| 109 | I != E; ++I) |
| 110 | PrintStmt(*I); |
| 111 | |
| 112 | Indent() << "}"; |
| 113 | } |
| 114 | |
| 115 | void StmtPrinter::PrintRawDecl(Decl *D) { |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 116 | D->print(OS, Policy, IndentLevel); |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Ted Kremenek | ecd64c5 | 2008-10-06 18:39:36 +0000 | [diff] [blame] | 119 | void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) { |
Eli Friedman | db23b15 | 2009-05-30 00:19:54 +0000 | [diff] [blame] | 120 | DeclStmt::decl_iterator Begin = S->decl_begin(), End = S->decl_end(); |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 121 | llvm::SmallVector<Decl*, 2> Decls; |
| 122 | for ( ; Begin != End; ++Begin) |
| 123 | Decls.push_back(*Begin); |
Eli Friedman | db23b15 | 2009-05-30 00:19:54 +0000 | [diff] [blame] | 124 | |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 125 | Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel); |
Ted Kremenek | ecd64c5 | 2008-10-06 18:39:36 +0000 | [diff] [blame] | 126 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 127 | |
| 128 | void StmtPrinter::VisitNullStmt(NullStmt *Node) { |
| 129 | Indent() << ";\n"; |
| 130 | } |
| 131 | |
| 132 | void StmtPrinter::VisitDeclStmt(DeclStmt *Node) { |
Eli Friedman | db23b15 | 2009-05-30 00:19:54 +0000 | [diff] [blame] | 133 | Indent(); |
| 134 | PrintRawDeclStmt(Node); |
| 135 | OS << ";\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) { |
| 139 | Indent(); |
| 140 | PrintRawCompoundStmt(Node); |
| 141 | OS << "\n"; |
| 142 | } |
| 143 | |
| 144 | void StmtPrinter::VisitCaseStmt(CaseStmt *Node) { |
| 145 | Indent(-1) << "case "; |
| 146 | PrintExpr(Node->getLHS()); |
| 147 | if (Node->getRHS()) { |
| 148 | OS << " ... "; |
| 149 | PrintExpr(Node->getRHS()); |
| 150 | } |
| 151 | OS << ":\n"; |
| 152 | |
| 153 | PrintStmt(Node->getSubStmt(), 0); |
| 154 | } |
| 155 | |
| 156 | void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) { |
| 157 | Indent(-1) << "default:\n"; |
| 158 | PrintStmt(Node->getSubStmt(), 0); |
| 159 | } |
| 160 | |
| 161 | void StmtPrinter::VisitLabelStmt(LabelStmt *Node) { |
| 162 | Indent(-1) << Node->getName() << ":\n"; |
| 163 | PrintStmt(Node->getSubStmt(), 0); |
| 164 | } |
| 165 | |
| 166 | void StmtPrinter::PrintRawIfStmt(IfStmt *If) { |
Sebastian Redl | bfee9b2 | 2009-02-07 20:05:48 +0000 | [diff] [blame] | 167 | OS << "if ("; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 168 | PrintExpr(If->getCond()); |
Sebastian Redl | bfee9b2 | 2009-02-07 20:05:48 +0000 | [diff] [blame] | 169 | OS << ')'; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 170 | |
| 171 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) { |
| 172 | OS << ' '; |
| 173 | PrintRawCompoundStmt(CS); |
| 174 | OS << (If->getElse() ? ' ' : '\n'); |
| 175 | } else { |
| 176 | OS << '\n'; |
| 177 | PrintStmt(If->getThen()); |
| 178 | if (If->getElse()) Indent(); |
| 179 | } |
| 180 | |
| 181 | if (Stmt *Else = If->getElse()) { |
| 182 | OS << "else"; |
| 183 | |
| 184 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) { |
| 185 | OS << ' '; |
| 186 | PrintRawCompoundStmt(CS); |
| 187 | OS << '\n'; |
| 188 | } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) { |
| 189 | OS << ' '; |
| 190 | PrintRawIfStmt(ElseIf); |
| 191 | } else { |
| 192 | OS << '\n'; |
| 193 | PrintStmt(If->getElse()); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | void StmtPrinter::VisitIfStmt(IfStmt *If) { |
| 199 | Indent(); |
| 200 | PrintRawIfStmt(If); |
| 201 | } |
| 202 | |
| 203 | void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) { |
| 204 | Indent() << "switch ("; |
| 205 | PrintExpr(Node->getCond()); |
| 206 | OS << ")"; |
| 207 | |
| 208 | // Pretty print compoundstmt bodies (very common). |
| 209 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { |
| 210 | OS << " "; |
| 211 | PrintRawCompoundStmt(CS); |
| 212 | OS << "\n"; |
| 213 | } else { |
| 214 | OS << "\n"; |
| 215 | PrintStmt(Node->getBody()); |
| 216 | } |
| 217 | } |
| 218 | |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 219 | void StmtPrinter::VisitSwitchCase(SwitchCase*) { |
| 220 | assert(0 && "SwitchCase is an abstract class"); |
| 221 | } |
| 222 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 223 | void StmtPrinter::VisitWhileStmt(WhileStmt *Node) { |
| 224 | Indent() << "while ("; |
| 225 | PrintExpr(Node->getCond()); |
| 226 | OS << ")\n"; |
| 227 | PrintStmt(Node->getBody()); |
| 228 | } |
| 229 | |
| 230 | void StmtPrinter::VisitDoStmt(DoStmt *Node) { |
Chris Lattner | 8bdcc47 | 2007-09-15 21:49:37 +0000 | [diff] [blame] | 231 | Indent() << "do "; |
| 232 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { |
| 233 | PrintRawCompoundStmt(CS); |
| 234 | OS << " "; |
| 235 | } else { |
| 236 | OS << "\n"; |
| 237 | PrintStmt(Node->getBody()); |
| 238 | Indent(); |
| 239 | } |
| 240 | |
Eli Friedman | b3e2296 | 2009-05-17 01:05:34 +0000 | [diff] [blame] | 241 | OS << "while ("; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 242 | PrintExpr(Node->getCond()); |
Eli Friedman | b3e2296 | 2009-05-17 01:05:34 +0000 | [diff] [blame] | 243 | OS << ");\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | void StmtPrinter::VisitForStmt(ForStmt *Node) { |
| 247 | Indent() << "for ("; |
| 248 | if (Node->getInit()) { |
| 249 | if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit())) |
Ted Kremenek | ecd64c5 | 2008-10-06 18:39:36 +0000 | [diff] [blame] | 250 | PrintRawDeclStmt(DS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 251 | else |
| 252 | PrintExpr(cast<Expr>(Node->getInit())); |
| 253 | } |
Chris Lattner | 8bdcc47 | 2007-09-15 21:49:37 +0000 | [diff] [blame] | 254 | OS << ";"; |
| 255 | if (Node->getCond()) { |
| 256 | OS << " "; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 257 | PrintExpr(Node->getCond()); |
Chris Lattner | 8bdcc47 | 2007-09-15 21:49:37 +0000 | [diff] [blame] | 258 | } |
| 259 | OS << ";"; |
| 260 | if (Node->getInc()) { |
| 261 | OS << " "; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 262 | PrintExpr(Node->getInc()); |
Chris Lattner | 8bdcc47 | 2007-09-15 21:49:37 +0000 | [diff] [blame] | 263 | } |
| 264 | OS << ") "; |
| 265 | |
| 266 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { |
| 267 | PrintRawCompoundStmt(CS); |
| 268 | OS << "\n"; |
| 269 | } else { |
| 270 | OS << "\n"; |
| 271 | PrintStmt(Node->getBody()); |
| 272 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 275 | void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) { |
Fariborz Jahanian | 0196cab | 2008-01-02 22:54:34 +0000 | [diff] [blame] | 276 | Indent() << "for ("; |
| 277 | if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement())) |
Ted Kremenek | ecd64c5 | 2008-10-06 18:39:36 +0000 | [diff] [blame] | 278 | PrintRawDeclStmt(DS); |
Fariborz Jahanian | 0196cab | 2008-01-02 22:54:34 +0000 | [diff] [blame] | 279 | else |
| 280 | PrintExpr(cast<Expr>(Node->getElement())); |
| 281 | OS << " in "; |
| 282 | PrintExpr(Node->getCollection()); |
| 283 | OS << ") "; |
| 284 | |
| 285 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { |
| 286 | PrintRawCompoundStmt(CS); |
| 287 | OS << "\n"; |
| 288 | } else { |
| 289 | OS << "\n"; |
| 290 | PrintStmt(Node->getBody()); |
| 291 | } |
| 292 | } |
| 293 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 294 | void StmtPrinter::VisitGotoStmt(GotoStmt *Node) { |
| 295 | Indent() << "goto " << Node->getLabel()->getName() << ";\n"; |
| 296 | } |
| 297 | |
| 298 | void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) { |
| 299 | Indent() << "goto *"; |
| 300 | PrintExpr(Node->getTarget()); |
| 301 | OS << ";\n"; |
| 302 | } |
| 303 | |
| 304 | void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) { |
| 305 | Indent() << "continue;\n"; |
| 306 | } |
| 307 | |
| 308 | void StmtPrinter::VisitBreakStmt(BreakStmt *Node) { |
| 309 | Indent() << "break;\n"; |
| 310 | } |
| 311 | |
| 312 | |
| 313 | void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) { |
| 314 | Indent() << "return"; |
| 315 | if (Node->getRetValue()) { |
| 316 | OS << " "; |
| 317 | PrintExpr(Node->getRetValue()); |
| 318 | } |
| 319 | OS << ";\n"; |
| 320 | } |
| 321 | |
Chris Lattner | fe79595 | 2007-10-29 04:04:16 +0000 | [diff] [blame] | 322 | |
| 323 | void StmtPrinter::VisitAsmStmt(AsmStmt *Node) { |
Anders Carlsson | 39c47b5 | 2007-11-23 23:12:25 +0000 | [diff] [blame] | 324 | Indent() << "asm "; |
| 325 | |
| 326 | if (Node->isVolatile()) |
| 327 | OS << "volatile "; |
| 328 | |
| 329 | OS << "("; |
Anders Carlsson | 6a0ef4b | 2007-11-20 19:21:03 +0000 | [diff] [blame] | 330 | VisitStringLiteral(Node->getAsmString()); |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 331 | |
| 332 | // Outputs |
| 333 | if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 || |
| 334 | Node->getNumClobbers() != 0) |
| 335 | OS << " : "; |
| 336 | |
| 337 | for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) { |
| 338 | if (i != 0) |
| 339 | OS << ", "; |
| 340 | |
| 341 | if (!Node->getOutputName(i).empty()) { |
| 342 | OS << '['; |
| 343 | OS << Node->getOutputName(i); |
| 344 | OS << "] "; |
| 345 | } |
| 346 | |
Chris Lattner | b327793 | 2009-03-10 04:59:06 +0000 | [diff] [blame] | 347 | VisitStringLiteral(Node->getOutputConstraintLiteral(i)); |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 348 | OS << " "; |
| 349 | Visit(Node->getOutputExpr(i)); |
| 350 | } |
| 351 | |
| 352 | // Inputs |
| 353 | if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0) |
| 354 | OS << " : "; |
| 355 | |
| 356 | for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) { |
| 357 | if (i != 0) |
| 358 | OS << ", "; |
| 359 | |
| 360 | if (!Node->getInputName(i).empty()) { |
| 361 | OS << '['; |
| 362 | OS << Node->getInputName(i); |
| 363 | OS << "] "; |
| 364 | } |
| 365 | |
Chris Lattner | b327793 | 2009-03-10 04:59:06 +0000 | [diff] [blame] | 366 | VisitStringLiteral(Node->getInputConstraintLiteral(i)); |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 367 | OS << " "; |
| 368 | Visit(Node->getInputExpr(i)); |
| 369 | } |
| 370 | |
| 371 | // Clobbers |
| 372 | if (Node->getNumClobbers() != 0) |
| 373 | OS << " : "; |
| 374 | |
| 375 | for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) { |
| 376 | if (i != 0) |
| 377 | OS << ", "; |
| 378 | |
| 379 | VisitStringLiteral(Node->getClobber(i)); |
| 380 | } |
| 381 | |
Anders Carlsson | 6a0ef4b | 2007-11-20 19:21:03 +0000 | [diff] [blame] | 382 | OS << ");\n"; |
Chris Lattner | fe79595 | 2007-10-29 04:04:16 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 385 | void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) { |
Fariborz Jahanian | 7794cb8 | 2007-11-02 18:16:07 +0000 | [diff] [blame] | 386 | Indent() << "@try"; |
| 387 | if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) { |
| 388 | PrintRawCompoundStmt(TS); |
| 389 | OS << "\n"; |
| 390 | } |
| 391 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 392 | for (ObjCAtCatchStmt *catchStmt = |
| 393 | static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts()); |
Fariborz Jahanian | 7794cb8 | 2007-11-02 18:16:07 +0000 | [diff] [blame] | 394 | catchStmt; |
| 395 | catchStmt = |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 396 | static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) { |
Fariborz Jahanian | 7794cb8 | 2007-11-02 18:16:07 +0000 | [diff] [blame] | 397 | Indent() << "@catch("; |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 398 | if (catchStmt->getCatchParamDecl()) { |
| 399 | if (Decl *DS = catchStmt->getCatchParamDecl()) |
| 400 | PrintRawDecl(DS); |
Fariborz Jahanian | 7794cb8 | 2007-11-02 18:16:07 +0000 | [diff] [blame] | 401 | } |
| 402 | OS << ")"; |
| 403 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) |
| 404 | { |
| 405 | PrintRawCompoundStmt(CS); |
| 406 | OS << "\n"; |
| 407 | } |
| 408 | } |
| 409 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 410 | if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>( |
Fariborz Jahanian | 1e7eab4 | 2007-11-07 00:46:42 +0000 | [diff] [blame] | 411 | Node->getFinallyStmt())) { |
| 412 | Indent() << "@finally"; |
| 413 | PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody())); |
Fariborz Jahanian | 7794cb8 | 2007-11-02 18:16:07 +0000 | [diff] [blame] | 414 | OS << "\n"; |
| 415 | } |
Fariborz Jahanian | b210bd0 | 2007-11-01 21:12:44 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 418 | void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) { |
Fariborz Jahanian | b210bd0 | 2007-11-01 21:12:44 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 421 | void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) { |
Fariborz Jahanian | b210bd0 | 2007-11-01 21:12:44 +0000 | [diff] [blame] | 422 | Indent() << "@catch (...) { /* todo */ } \n"; |
| 423 | } |
| 424 | |
Fariborz Jahanian | 78a677b | 2008-01-30 17:38:29 +0000 | [diff] [blame] | 425 | void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) { |
Fariborz Jahanian | 39f8f15 | 2007-11-07 02:00:49 +0000 | [diff] [blame] | 426 | Indent() << "@throw"; |
| 427 | if (Node->getThrowExpr()) { |
| 428 | OS << " "; |
| 429 | PrintExpr(Node->getThrowExpr()); |
| 430 | } |
| 431 | OS << ";\n"; |
| 432 | } |
| 433 | |
Fariborz Jahanian | 78a677b | 2008-01-30 17:38:29 +0000 | [diff] [blame] | 434 | void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) { |
Fariborz Jahanian | c385c90 | 2008-01-29 18:21:32 +0000 | [diff] [blame] | 435 | Indent() << "@synchronized ("; |
| 436 | PrintExpr(Node->getSynchExpr()); |
| 437 | OS << ")"; |
Fariborz Jahanian | 78a677b | 2008-01-30 17:38:29 +0000 | [diff] [blame] | 438 | PrintRawCompoundStmt(Node->getSynchBody()); |
| 439 | OS << "\n"; |
Fariborz Jahanian | c385c90 | 2008-01-29 18:21:32 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 442 | void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) { |
| 443 | OS << "catch ("; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 444 | if (Decl *ExDecl = Node->getExceptionDecl()) |
| 445 | PrintRawDecl(ExDecl); |
| 446 | else |
| 447 | OS << "..."; |
| 448 | OS << ") "; |
| 449 | PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock())); |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) { |
| 453 | Indent(); |
| 454 | PrintRawCXXCatchStmt(Node); |
| 455 | OS << "\n"; |
| 456 | } |
| 457 | |
| 458 | void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) { |
| 459 | Indent() << "try "; |
| 460 | PrintRawCompoundStmt(Node->getTryBlock()); |
| 461 | for(unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) { |
| 462 | OS << " "; |
| 463 | PrintRawCXXCatchStmt(Node->getHandler(i)); |
| 464 | } |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 465 | OS << "\n"; |
| 466 | } |
| 467 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 468 | //===----------------------------------------------------------------------===// |
| 469 | // Expr printing methods. |
| 470 | //===----------------------------------------------------------------------===// |
| 471 | |
| 472 | void StmtPrinter::VisitExpr(Expr *Node) { |
| 473 | OS << "<<unknown expr type>>"; |
| 474 | } |
| 475 | |
| 476 | void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) { |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 477 | OS << Node->getDecl()->getNameAsString(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 480 | void StmtPrinter::VisitQualifiedDeclRefExpr(QualifiedDeclRefExpr *Node) { |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 481 | NamedDecl *D = Node->getDecl(); |
| 482 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 483 | Node->getQualifier()->print(OS, Policy); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 484 | OS << D->getNameAsString(); |
| 485 | } |
| 486 | |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 487 | void StmtPrinter::VisitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *Node) { |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 488 | Node->getQualifier()->print(OS, Policy); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 489 | OS << Node->getDeclName().getAsString(); |
| 490 | } |
| 491 | |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 492 | void StmtPrinter::VisitTemplateIdRefExpr(TemplateIdRefExpr *Node) { |
| 493 | if (Node->getQualifier()) |
| 494 | Node->getQualifier()->print(OS, Policy); |
| 495 | Node->getTemplateName().print(OS, Policy, true); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 496 | OS << TemplateSpecializationType::PrintTemplateArgumentList( |
| 497 | Node->getTemplateArgs(), |
| 498 | Node->getNumTemplateArgs(), |
| 499 | Policy); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Steve Naroff | 7779db4 | 2007-11-12 14:29:37 +0000 | [diff] [blame] | 502 | void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { |
Fariborz Jahanian | 232220c | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 503 | if (Node->getBase()) { |
| 504 | PrintExpr(Node->getBase()); |
| 505 | OS << (Node->isArrow() ? "->" : "."); |
| 506 | } |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 507 | OS << Node->getDecl()->getNameAsString(); |
Steve Naroff | 7779db4 | 2007-11-12 14:29:37 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Steve Naroff | ae78407 | 2008-05-30 00:40:33 +0000 | [diff] [blame] | 510 | void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) { |
| 511 | if (Node->getBase()) { |
| 512 | PrintExpr(Node->getBase()); |
| 513 | OS << "."; |
| 514 | } |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 515 | OS << Node->getProperty()->getNameAsCString(); |
Steve Naroff | ae78407 | 2008-05-30 00:40:33 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 518 | void StmtPrinter::VisitObjCImplicitSetterGetterRefExpr( |
| 519 | ObjCImplicitSetterGetterRefExpr *Node) { |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 520 | if (Node->getBase()) { |
| 521 | PrintExpr(Node->getBase()); |
| 522 | OS << "."; |
| 523 | } |
Fariborz Jahanian | 154440e | 2009-08-18 20:50:23 +0000 | [diff] [blame] | 524 | if (Node->getGetterMethod()) |
| 525 | OS << Node->getGetterMethod()->getNameAsString(); |
| 526 | |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 529 | void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) { |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 530 | switch (Node->getIdentType()) { |
| 531 | default: |
| 532 | assert(0 && "unknown case"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 533 | case PredefinedExpr::Func: |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 534 | OS << "__func__"; |
| 535 | break; |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 536 | case PredefinedExpr::Function: |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 537 | OS << "__FUNCTION__"; |
| 538 | break; |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 539 | case PredefinedExpr::PrettyFunction: |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 540 | OS << "__PRETTY_FUNCTION__"; |
| 541 | break; |
| 542 | } |
| 543 | } |
| 544 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 545 | void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) { |
Chris Lattner | b0a721a | 2007-07-13 05:18:11 +0000 | [diff] [blame] | 546 | unsigned value = Node->getValue(); |
Chris Lattner | c250aae | 2008-06-07 22:35:38 +0000 | [diff] [blame] | 547 | if (Node->isWide()) |
| 548 | OS << "L"; |
Chris Lattner | 8bf9f07 | 2007-07-13 23:58:20 +0000 | [diff] [blame] | 549 | switch (value) { |
| 550 | case '\\': |
| 551 | OS << "'\\\\'"; |
| 552 | break; |
| 553 | case '\'': |
| 554 | OS << "'\\''"; |
| 555 | break; |
| 556 | case '\a': |
| 557 | // TODO: K&R: the meaning of '\\a' is different in traditional C |
| 558 | OS << "'\\a'"; |
| 559 | break; |
| 560 | case '\b': |
| 561 | OS << "'\\b'"; |
| 562 | break; |
| 563 | // Nonstandard escape sequence. |
| 564 | /*case '\e': |
| 565 | OS << "'\\e'"; |
| 566 | break;*/ |
| 567 | case '\f': |
| 568 | OS << "'\\f'"; |
| 569 | break; |
| 570 | case '\n': |
| 571 | OS << "'\\n'"; |
| 572 | break; |
| 573 | case '\r': |
| 574 | OS << "'\\r'"; |
| 575 | break; |
| 576 | case '\t': |
| 577 | OS << "'\\t'"; |
| 578 | break; |
| 579 | case '\v': |
| 580 | OS << "'\\v'"; |
| 581 | break; |
| 582 | default: |
Ted Kremenek | 471733d | 2008-02-23 00:52:04 +0000 | [diff] [blame] | 583 | if (value < 256 && isprint(value)) { |
Chris Lattner | 8bf9f07 | 2007-07-13 23:58:20 +0000 | [diff] [blame] | 584 | OS << "'" << (char)value << "'"; |
| 585 | } else if (value < 256) { |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 586 | OS << "'\\x" << llvm::format("%x", value) << "'"; |
Chris Lattner | 8bf9f07 | 2007-07-13 23:58:20 +0000 | [diff] [blame] | 587 | } else { |
| 588 | // FIXME what to really do here? |
| 589 | OS << value; |
| 590 | } |
Chris Lattner | b0a721a | 2007-07-13 05:18:11 +0000 | [diff] [blame] | 591 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) { |
| 595 | bool isSigned = Node->getType()->isSignedIntegerType(); |
| 596 | OS << Node->getValue().toString(10, isSigned); |
| 597 | |
| 598 | // Emit suffixes. Integer literals are always a builtin integer type. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 599 | switch (Node->getType()->getAsBuiltinType()->getKind()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 600 | default: assert(0 && "Unexpected type for integer literal!"); |
| 601 | case BuiltinType::Int: break; // no suffix. |
| 602 | case BuiltinType::UInt: OS << 'U'; break; |
| 603 | case BuiltinType::Long: OS << 'L'; break; |
| 604 | case BuiltinType::ULong: OS << "UL"; break; |
| 605 | case BuiltinType::LongLong: OS << "LL"; break; |
| 606 | case BuiltinType::ULongLong: OS << "ULL"; break; |
| 607 | } |
| 608 | } |
| 609 | void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) { |
Chris Lattner | 86e499d | 2007-08-01 00:23:58 +0000 | [diff] [blame] | 610 | // FIXME: print value more precisely. |
Chris Lattner | da8249e | 2008-06-07 22:13:43 +0000 | [diff] [blame] | 611 | OS << Node->getValueAsApproximateDouble(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 612 | } |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 613 | |
| 614 | void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) { |
| 615 | PrintExpr(Node->getSubExpr()); |
| 616 | OS << "i"; |
| 617 | } |
| 618 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 619 | void StmtPrinter::VisitStringLiteral(StringLiteral *Str) { |
| 620 | if (Str->isWide()) OS << 'L'; |
| 621 | OS << '"'; |
Anders Carlsson | ee98ac5 | 2007-10-15 02:50:23 +0000 | [diff] [blame] | 622 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 623 | // FIXME: this doesn't print wstrings right. |
| 624 | for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) { |
Chris Lattner | 9a81c87 | 2009-01-16 19:25:18 +0000 | [diff] [blame] | 625 | unsigned char Char = Str->getStrData()[i]; |
| 626 | |
| 627 | switch (Char) { |
| 628 | default: |
| 629 | if (isprint(Char)) |
| 630 | OS << (char)Char; |
| 631 | else // Output anything hard as an octal escape. |
| 632 | OS << '\\' |
| 633 | << (char)('0'+ ((Char >> 6) & 7)) |
| 634 | << (char)('0'+ ((Char >> 3) & 7)) |
| 635 | << (char)('0'+ ((Char >> 0) & 7)); |
| 636 | break; |
| 637 | // Handle some common non-printable cases to make dumps prettier. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 638 | case '\\': OS << "\\\\"; break; |
| 639 | case '"': OS << "\\\""; break; |
| 640 | case '\n': OS << "\\n"; break; |
| 641 | case '\t': OS << "\\t"; break; |
| 642 | case '\a': OS << "\\a"; break; |
| 643 | case '\b': OS << "\\b"; break; |
| 644 | } |
| 645 | } |
| 646 | OS << '"'; |
| 647 | } |
| 648 | void StmtPrinter::VisitParenExpr(ParenExpr *Node) { |
| 649 | OS << "("; |
| 650 | PrintExpr(Node->getSubExpr()); |
| 651 | OS << ")"; |
| 652 | } |
| 653 | void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) { |
Chris Lattner | 296bf19 | 2007-08-23 21:46:40 +0000 | [diff] [blame] | 654 | if (!Node->isPostfix()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 655 | OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); |
Chris Lattner | 296bf19 | 2007-08-23 21:46:40 +0000 | [diff] [blame] | 656 | |
Eli Friedman | 7df71ac | 2009-06-14 22:39:26 +0000 | [diff] [blame] | 657 | // Print a space if this is an "identifier operator" like __real, or if |
| 658 | // it might be concatenated incorrectly like '+'. |
Chris Lattner | 296bf19 | 2007-08-23 21:46:40 +0000 | [diff] [blame] | 659 | switch (Node->getOpcode()) { |
| 660 | default: break; |
Chris Lattner | 296bf19 | 2007-08-23 21:46:40 +0000 | [diff] [blame] | 661 | case UnaryOperator::Real: |
| 662 | case UnaryOperator::Imag: |
| 663 | case UnaryOperator::Extension: |
| 664 | OS << ' '; |
| 665 | break; |
Eli Friedman | 7df71ac | 2009-06-14 22:39:26 +0000 | [diff] [blame] | 666 | case UnaryOperator::Plus: |
| 667 | case UnaryOperator::Minus: |
| 668 | if (isa<UnaryOperator>(Node->getSubExpr())) |
| 669 | OS << ' '; |
| 670 | break; |
Chris Lattner | 296bf19 | 2007-08-23 21:46:40 +0000 | [diff] [blame] | 671 | } |
| 672 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 673 | PrintExpr(Node->getSubExpr()); |
| 674 | |
| 675 | if (Node->isPostfix()) |
| 676 | OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 677 | } |
Chris Lattner | 704fe35 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 678 | |
| 679 | bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) { |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 680 | if (isa<UnaryOperator>(E)) { |
Chris Lattner | 704fe35 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 681 | // Base case, print the type and comma. |
| 682 | OS << E->getType().getAsString() << ", "; |
| 683 | return true; |
| 684 | } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) { |
| 685 | PrintOffsetOfDesignator(ASE->getLHS()); |
| 686 | OS << "["; |
| 687 | PrintExpr(ASE->getRHS()); |
| 688 | OS << "]"; |
| 689 | return false; |
| 690 | } else { |
| 691 | MemberExpr *ME = cast<MemberExpr>(E); |
| 692 | bool IsFirst = PrintOffsetOfDesignator(ME->getBase()); |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 693 | OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString(); |
Chris Lattner | 704fe35 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 694 | return false; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) { |
| 699 | OS << "__builtin_offsetof("; |
| 700 | PrintOffsetOfDesignator(Node->getSubExpr()); |
| 701 | OS << ")"; |
| 702 | } |
| 703 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 704 | void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) { |
| 705 | OS << (Node->isSizeOf() ? "sizeof" : "__alignof"); |
| 706 | if (Node->isArgumentType()) |
| 707 | OS << "(" << Node->getArgumentType().getAsString() << ")"; |
| 708 | else { |
| 709 | OS << " "; |
| 710 | PrintExpr(Node->getArgumentExpr()); |
| 711 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 712 | } |
| 713 | void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) { |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 714 | PrintExpr(Node->getLHS()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 715 | OS << "["; |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 716 | PrintExpr(Node->getRHS()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 717 | OS << "]"; |
| 718 | } |
| 719 | |
| 720 | void StmtPrinter::VisitCallExpr(CallExpr *Call) { |
| 721 | PrintExpr(Call->getCallee()); |
| 722 | OS << "("; |
| 723 | for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 724 | if (isa<CXXDefaultArgExpr>(Call->getArg(i))) { |
| 725 | // Don't print any defaulted arguments |
| 726 | break; |
| 727 | } |
| 728 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 729 | if (i) OS << ", "; |
| 730 | PrintExpr(Call->getArg(i)); |
| 731 | } |
| 732 | OS << ")"; |
| 733 | } |
| 734 | void StmtPrinter::VisitMemberExpr(MemberExpr *Node) { |
Douglas Gregor | b3eef68 | 2009-01-08 22:45:41 +0000 | [diff] [blame] | 735 | // FIXME: Suppress printing implicit bases (like "this") |
| 736 | PrintExpr(Node->getBase()); |
| 737 | OS << (Node->isArrow() ? "->" : "."); |
| 738 | // FIXME: Suppress printing references to unnamed objects |
| 739 | // representing anonymous unions/structs |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 740 | if (NestedNameSpecifier *Qualifier = Node->getQualifier()) |
| 741 | Qualifier->print(OS, Policy); |
| 742 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 743 | OS << Node->getMemberDecl()->getNameAsString(); |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 744 | |
| 745 | if (Node->hasExplicitTemplateArgumentList()) |
| 746 | OS << TemplateSpecializationType::PrintTemplateArgumentList( |
| 747 | Node->getTemplateArgs(), |
| 748 | Node->getNumTemplateArgs(), |
| 749 | Policy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 750 | } |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 751 | void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) { |
| 752 | PrintExpr(Node->getBase()); |
| 753 | OS << (Node->isArrow() ? "->isa" : ".isa"); |
| 754 | } |
| 755 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 756 | void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) { |
Steve Naroff | 31a4584 | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 757 | PrintExpr(Node->getBase()); |
| 758 | OS << "."; |
| 759 | OS << Node->getAccessor().getName(); |
| 760 | } |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 761 | void StmtPrinter::VisitCastExpr(CastExpr *) { |
| 762 | assert(0 && "CastExpr is an abstract class"); |
| 763 | } |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 764 | void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) { |
| 765 | assert(0 && "ExplicitCastExpr is an abstract class"); |
| 766 | } |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 767 | void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) { |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 768 | OS << "(" << Node->getType().getAsString() << ")"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 769 | PrintExpr(Node->getSubExpr()); |
| 770 | } |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 771 | void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) { |
| 772 | OS << "(" << Node->getType().getAsString() << ")"; |
| 773 | PrintExpr(Node->getInitializer()); |
| 774 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 775 | void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) { |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 776 | // No need to print anything, simply forward to the sub expression. |
| 777 | PrintExpr(Node->getSubExpr()); |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 778 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 779 | void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) { |
| 780 | PrintExpr(Node->getLHS()); |
| 781 | OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; |
| 782 | PrintExpr(Node->getRHS()); |
| 783 | } |
Chris Lattner | eb14fe8 | 2007-08-25 02:00:02 +0000 | [diff] [blame] | 784 | void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) { |
| 785 | PrintExpr(Node->getLHS()); |
| 786 | OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; |
| 787 | PrintExpr(Node->getRHS()); |
| 788 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 789 | void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { |
| 790 | PrintExpr(Node->getCond()); |
Ted Kremenek | 8e911c4 | 2007-11-26 18:27:54 +0000 | [diff] [blame] | 791 | |
| 792 | if (Node->getLHS()) { |
| 793 | OS << " ? "; |
| 794 | PrintExpr(Node->getLHS()); |
| 795 | OS << " : "; |
| 796 | } |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 797 | else { // Handle GCC extension where LHS can be NULL. |
Ted Kremenek | 8e911c4 | 2007-11-26 18:27:54 +0000 | [diff] [blame] | 798 | OS << " ?: "; |
| 799 | } |
| 800 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 801 | PrintExpr(Node->getRHS()); |
| 802 | } |
| 803 | |
| 804 | // GNU extensions. |
| 805 | |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 806 | void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 807 | OS << "&&" << Node->getLabel()->getName(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 810 | void StmtPrinter::VisitStmtExpr(StmtExpr *E) { |
| 811 | OS << "("; |
| 812 | PrintRawCompoundStmt(E->getSubStmt()); |
| 813 | OS << ")"; |
| 814 | } |
| 815 | |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 816 | void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) { |
| 817 | OS << "__builtin_types_compatible_p("; |
| 818 | OS << Node->getArgType1().getAsString() << ","; |
| 819 | OS << Node->getArgType2().getAsString() << ")"; |
| 820 | } |
| 821 | |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 822 | void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) { |
| 823 | OS << "__builtin_choose_expr("; |
| 824 | PrintExpr(Node->getCond()); |
Chris Lattner | 94f05e3 | 2007-08-04 00:20:15 +0000 | [diff] [blame] | 825 | OS << ", "; |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 826 | PrintExpr(Node->getLHS()); |
Chris Lattner | 94f05e3 | 2007-08-04 00:20:15 +0000 | [diff] [blame] | 827 | OS << ", "; |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 828 | PrintExpr(Node->getRHS()); |
| 829 | OS << ")"; |
| 830 | } |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 831 | |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 832 | void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) { |
| 833 | OS << "__null"; |
| 834 | } |
| 835 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 836 | void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) { |
| 837 | OS << "__builtin_shufflevector("; |
| 838 | for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) { |
| 839 | if (i) OS << ", "; |
| 840 | PrintExpr(Node->getExpr(i)); |
| 841 | } |
| 842 | OS << ")"; |
| 843 | } |
| 844 | |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 845 | void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 846 | if (Node->getSyntacticForm()) { |
| 847 | Visit(Node->getSyntacticForm()); |
| 848 | return; |
| 849 | } |
| 850 | |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 851 | OS << "{ "; |
| 852 | for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { |
| 853 | if (i) OS << ", "; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 854 | if (Node->getInit(i)) |
| 855 | PrintExpr(Node->getInit(i)); |
| 856 | else |
| 857 | OS << "0"; |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 858 | } |
| 859 | OS << " }"; |
| 860 | } |
| 861 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 862 | void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) { |
| 863 | OS << "( "; |
| 864 | for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) { |
| 865 | if (i) OS << ", "; |
| 866 | PrintExpr(Node->getExpr(i)); |
| 867 | } |
| 868 | OS << " )"; |
| 869 | } |
| 870 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 871 | void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 872 | for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(), |
| 873 | DEnd = Node->designators_end(); |
| 874 | D != DEnd; ++D) { |
| 875 | if (D->isFieldDesignator()) { |
| 876 | if (D->getDotLoc().isInvalid()) |
| 877 | OS << D->getFieldName()->getName() << ":"; |
| 878 | else |
| 879 | OS << "." << D->getFieldName()->getName(); |
| 880 | } else { |
| 881 | OS << "["; |
| 882 | if (D->isArrayDesignator()) { |
| 883 | PrintExpr(Node->getArrayIndex(*D)); |
| 884 | } else { |
| 885 | PrintExpr(Node->getArrayRangeStart(*D)); |
| 886 | OS << " ... "; |
| 887 | PrintExpr(Node->getArrayRangeEnd(*D)); |
| 888 | } |
| 889 | OS << "]"; |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | OS << " = "; |
| 894 | PrintExpr(Node->getInit()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 897 | void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) { |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 898 | if (Policy.LangOpts.CPlusPlus) |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 899 | OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()"; |
| 900 | else { |
| 901 | OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")"; |
| 902 | if (Node->getType()->isRecordType()) |
| 903 | OS << "{}"; |
| 904 | else |
| 905 | OS << 0; |
| 906 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 909 | void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) { |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 910 | OS << "__builtin_va_arg("; |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 911 | PrintExpr(Node->getSubExpr()); |
| 912 | OS << ", "; |
| 913 | OS << Node->getType().getAsString(); |
| 914 | OS << ")"; |
| 915 | } |
| 916 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 917 | // C++ |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 918 | void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { |
| 919 | const char *OpStrings[NUM_OVERLOADED_OPERATORS] = { |
| 920 | "", |
| 921 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 922 | Spelling, |
| 923 | #include "clang/Basic/OperatorKinds.def" |
| 924 | }; |
| 925 | |
| 926 | OverloadedOperatorKind Kind = Node->getOperator(); |
| 927 | if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { |
| 928 | if (Node->getNumArgs() == 1) { |
| 929 | OS << OpStrings[Kind] << ' '; |
| 930 | PrintExpr(Node->getArg(0)); |
| 931 | } else { |
| 932 | PrintExpr(Node->getArg(0)); |
| 933 | OS << ' ' << OpStrings[Kind]; |
| 934 | } |
| 935 | } else if (Kind == OO_Call) { |
| 936 | PrintExpr(Node->getArg(0)); |
| 937 | OS << '('; |
| 938 | for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) { |
| 939 | if (ArgIdx > 1) |
| 940 | OS << ", "; |
| 941 | if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx))) |
| 942 | PrintExpr(Node->getArg(ArgIdx)); |
| 943 | } |
| 944 | OS << ')'; |
| 945 | } else if (Kind == OO_Subscript) { |
| 946 | PrintExpr(Node->getArg(0)); |
| 947 | OS << '['; |
| 948 | PrintExpr(Node->getArg(1)); |
| 949 | OS << ']'; |
| 950 | } else if (Node->getNumArgs() == 1) { |
| 951 | OS << OpStrings[Kind] << ' '; |
| 952 | PrintExpr(Node->getArg(0)); |
| 953 | } else if (Node->getNumArgs() == 2) { |
| 954 | PrintExpr(Node->getArg(0)); |
| 955 | OS << ' ' << OpStrings[Kind] << ' '; |
| 956 | PrintExpr(Node->getArg(1)); |
| 957 | } else { |
| 958 | assert(false && "unknown overloaded operator"); |
| 959 | } |
| 960 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 961 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 962 | void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) { |
| 963 | VisitCallExpr(cast<CallExpr>(Node)); |
| 964 | } |
| 965 | |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 966 | void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) { |
| 967 | OS << Node->getCastName() << '<'; |
| 968 | OS << Node->getTypeAsWritten().getAsString() << ">("; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 969 | PrintExpr(Node->getSubExpr()); |
| 970 | OS << ")"; |
| 971 | } |
| 972 | |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 973 | void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) { |
| 974 | VisitCXXNamedCastExpr(Node); |
| 975 | } |
| 976 | |
| 977 | void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) { |
| 978 | VisitCXXNamedCastExpr(Node); |
| 979 | } |
| 980 | |
| 981 | void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) { |
| 982 | VisitCXXNamedCastExpr(Node); |
| 983 | } |
| 984 | |
| 985 | void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) { |
| 986 | VisitCXXNamedCastExpr(Node); |
| 987 | } |
| 988 | |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 989 | void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) { |
| 990 | OS << "typeid("; |
| 991 | if (Node->isTypeOperand()) { |
| 992 | OS << Node->getTypeOperand().getAsString(); |
| 993 | } else { |
| 994 | PrintExpr(Node->getExprOperand()); |
| 995 | } |
| 996 | OS << ")"; |
| 997 | } |
| 998 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 999 | void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { |
| 1000 | OS << (Node->getValue() ? "true" : "false"); |
| 1001 | } |
| 1002 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1003 | void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) { |
| 1004 | OS << "nullptr"; |
| 1005 | } |
| 1006 | |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 1007 | void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) { |
| 1008 | OS << "this"; |
| 1009 | } |
| 1010 | |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 1011 | void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) { |
| 1012 | if (Node->getSubExpr() == 0) |
| 1013 | OS << "throw"; |
| 1014 | else { |
| 1015 | OS << "throw "; |
| 1016 | PrintExpr(Node->getSubExpr()); |
| 1017 | } |
| 1018 | } |
| 1019 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1020 | void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) { |
| 1021 | // Nothing to print: we picked up the default argument |
| 1022 | } |
| 1023 | |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1024 | void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) { |
| 1025 | OS << Node->getType().getAsString(); |
| 1026 | OS << "("; |
| 1027 | PrintExpr(Node->getSubExpr()); |
| 1028 | OS << ")"; |
| 1029 | } |
| 1030 | |
Anders Carlsson | fceb0a8 | 2009-05-30 20:03:25 +0000 | [diff] [blame] | 1031 | void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) { |
| 1032 | PrintExpr(Node->getSubExpr()); |
| 1033 | } |
| 1034 | |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 1035 | void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) { |
| 1036 | OS << Node->getType().getAsString(); |
| 1037 | OS << "("; |
| 1038 | for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(), |
| 1039 | ArgEnd = Node->arg_end(); |
| 1040 | Arg != ArgEnd; ++Arg) { |
| 1041 | if (Arg != Node->arg_begin()) |
| 1042 | OS << ", "; |
| 1043 | PrintExpr(*Arg); |
| 1044 | } |
| 1045 | OS << ")"; |
| 1046 | } |
| 1047 | |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1048 | void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) { |
| 1049 | OS << Node->getType().getAsString() << "()"; |
| 1050 | } |
| 1051 | |
Argyrios Kyrtzidis | 9e922b1 | 2008-09-09 23:47:53 +0000 | [diff] [blame] | 1052 | void |
| 1053 | StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) { |
| 1054 | PrintRawDecl(E->getVarDecl()); |
| 1055 | } |
| 1056 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1057 | void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) { |
| 1058 | if (E->isGlobalNew()) |
| 1059 | OS << "::"; |
| 1060 | OS << "new "; |
| 1061 | unsigned NumPlace = E->getNumPlacementArgs(); |
| 1062 | if (NumPlace > 0) { |
| 1063 | OS << "("; |
| 1064 | PrintExpr(E->getPlacementArg(0)); |
| 1065 | for (unsigned i = 1; i < NumPlace; ++i) { |
| 1066 | OS << ", "; |
| 1067 | PrintExpr(E->getPlacementArg(i)); |
| 1068 | } |
| 1069 | OS << ") "; |
| 1070 | } |
| 1071 | if (E->isParenTypeId()) |
| 1072 | OS << "("; |
Sebastian Redl | 6fec648 | 2008-12-02 22:08:59 +0000 | [diff] [blame] | 1073 | std::string TypeS; |
| 1074 | if (Expr *Size = E->getArraySize()) { |
| 1075 | llvm::raw_string_ostream s(TypeS); |
Eli Friedman | 6e1a345 | 2009-05-30 05:32:46 +0000 | [diff] [blame] | 1076 | Size->printPretty(s, Context, Helper, Policy); |
Sebastian Redl | 6fec648 | 2008-12-02 22:08:59 +0000 | [diff] [blame] | 1077 | s.flush(); |
| 1078 | TypeS = "[" + TypeS + "]"; |
| 1079 | } |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1080 | E->getAllocatedType().getAsStringInternal(TypeS, Policy); |
Sebastian Redl | 6fec648 | 2008-12-02 22:08:59 +0000 | [diff] [blame] | 1081 | OS << TypeS; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1082 | if (E->isParenTypeId()) |
| 1083 | OS << ")"; |
| 1084 | |
| 1085 | if (E->hasInitializer()) { |
| 1086 | OS << "("; |
| 1087 | unsigned NumCons = E->getNumConstructorArgs(); |
| 1088 | if (NumCons > 0) { |
| 1089 | PrintExpr(E->getConstructorArg(0)); |
| 1090 | for (unsigned i = 1; i < NumCons; ++i) { |
| 1091 | OS << ", "; |
| 1092 | PrintExpr(E->getConstructorArg(i)); |
| 1093 | } |
| 1094 | } |
| 1095 | OS << ")"; |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 1100 | if (E->isGlobalDelete()) |
| 1101 | OS << "::"; |
| 1102 | OS << "delete "; |
| 1103 | if (E->isArrayForm()) |
| 1104 | OS << "[] "; |
| 1105 | PrintExpr(E->getArgument()); |
| 1106 | } |
| 1107 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 1108 | void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { |
| 1109 | PrintExpr(E->getBase()); |
| 1110 | if (E->isArrow()) |
| 1111 | OS << "->"; |
| 1112 | else |
| 1113 | OS << '.'; |
| 1114 | if (E->getQualifier()) |
| 1115 | E->getQualifier()->print(OS, Policy); |
| 1116 | |
| 1117 | std::string TypeS; |
| 1118 | E->getDestroyedType().getAsStringInternal(TypeS, Policy); |
| 1119 | OS << TypeS; |
| 1120 | } |
| 1121 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 1122 | void StmtPrinter::VisitUnresolvedFunctionNameExpr(UnresolvedFunctionNameExpr *E) { |
| 1123 | OS << E->getName().getAsString(); |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
Anders Carlsson | e349bea | 2009-04-23 02:32:43 +0000 | [diff] [blame] | 1126 | void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 1127 | // Nothing to print. |
| 1128 | } |
| 1129 | |
Anders Carlsson | 2d44e8a | 2009-05-01 22:18:43 +0000 | [diff] [blame] | 1130 | void StmtPrinter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) { |
Anders Carlsson | 02bbfa3 | 2009-04-24 22:47:04 +0000 | [diff] [blame] | 1131 | // Just forward to the sub expression. |
| 1132 | PrintExpr(E->getSubExpr()); |
| 1133 | } |
| 1134 | |
Douglas Gregor | d81e6ca | 2009-05-20 18:46:25 +0000 | [diff] [blame] | 1135 | void |
| 1136 | StmtPrinter::VisitCXXUnresolvedConstructExpr( |
| 1137 | CXXUnresolvedConstructExpr *Node) { |
| 1138 | OS << Node->getTypeAsWritten().getAsString(); |
| 1139 | OS << "("; |
| 1140 | for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(), |
| 1141 | ArgEnd = Node->arg_end(); |
| 1142 | Arg != ArgEnd; ++Arg) { |
| 1143 | if (Arg != Node->arg_begin()) |
| 1144 | OS << ", "; |
| 1145 | PrintExpr(*Arg); |
| 1146 | } |
| 1147 | OS << ")"; |
| 1148 | } |
| 1149 | |
Douglas Gregor | 1c0ca59 | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 1150 | void StmtPrinter::VisitCXXUnresolvedMemberExpr(CXXUnresolvedMemberExpr *Node) { |
| 1151 | PrintExpr(Node->getBase()); |
| 1152 | OS << (Node->isArrow() ? "->" : "."); |
Douglas Gregor | a38c687 | 2009-09-03 16:14:30 +0000 | [diff] [blame] | 1153 | if (NestedNameSpecifier *Qualifier = Node->getQualifier()) |
| 1154 | Qualifier->print(OS, Policy); |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 1155 | else if (Node->hasExplicitTemplateArgumentList()) |
| 1156 | // FIXME: Track use of "template" keyword explicitly? |
| 1157 | OS << "template "; |
| 1158 | |
Douglas Gregor | 1c0ca59 | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 1159 | OS << Node->getMember().getAsString(); |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 1160 | |
| 1161 | if (Node->hasExplicitTemplateArgumentList()) { |
| 1162 | OS << TemplateSpecializationType::PrintTemplateArgumentList( |
| 1163 | Node->getTemplateArgs(), |
| 1164 | Node->getNumTemplateArgs(), |
| 1165 | Policy); |
| 1166 | } |
Douglas Gregor | 1c0ca59 | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1169 | static const char *getTypeTraitName(UnaryTypeTrait UTT) { |
| 1170 | switch (UTT) { |
| 1171 | default: assert(false && "Unknown type trait"); |
| 1172 | case UTT_HasNothrowAssign: return "__has_nothrow_assign"; |
| 1173 | case UTT_HasNothrowCopy: return "__has_nothrow_copy"; |
| 1174 | case UTT_HasNothrowConstructor: return "__has_nothrow_constructor"; |
| 1175 | case UTT_HasTrivialAssign: return "__has_trivial_assign"; |
| 1176 | case UTT_HasTrivialCopy: return "__has_trivial_copy"; |
| 1177 | case UTT_HasTrivialConstructor: return "__has_trivial_constructor"; |
| 1178 | case UTT_HasTrivialDestructor: return "__has_trivial_destructor"; |
| 1179 | case UTT_HasVirtualDestructor: return "__has_virtual_destructor"; |
| 1180 | case UTT_IsAbstract: return "__is_abstract"; |
| 1181 | case UTT_IsClass: return "__is_class"; |
| 1182 | case UTT_IsEmpty: return "__is_empty"; |
| 1183 | case UTT_IsEnum: return "__is_enum"; |
| 1184 | case UTT_IsPOD: return "__is_pod"; |
| 1185 | case UTT_IsPolymorphic: return "__is_polymorphic"; |
| 1186 | case UTT_IsUnion: return "__is_union"; |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) { |
| 1191 | OS << getTypeTraitName(E->getTrait()) << "(" |
| 1192 | << E->getQueriedType().getAsString() << ")"; |
| 1193 | } |
| 1194 | |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1195 | // Obj-C |
| 1196 | |
| 1197 | void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { |
| 1198 | OS << "@"; |
| 1199 | VisitStringLiteral(Node->getString()); |
| 1200 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1201 | |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1202 | void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 1203 | OS << "@encode(" << Node->getEncodedType().getAsString() << ')'; |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1206 | void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) { |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 1207 | OS << "@selector(" << Node->getSelector().getAsString() << ')'; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1210 | void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) { |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 1211 | OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')'; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1214 | void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) { |
| 1215 | OS << "["; |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 1216 | Expr *receiver = Mess->getReceiver(); |
| 1217 | if (receiver) PrintExpr(receiver); |
| 1218 | else OS << Mess->getClassName()->getName(); |
Ted Kremenek | c29efd8 | 2008-05-02 17:32:38 +0000 | [diff] [blame] | 1219 | OS << ' '; |
Ted Kremenek | 97b7f26 | 2008-04-16 04:30:16 +0000 | [diff] [blame] | 1220 | Selector selector = Mess->getSelector(); |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 1221 | if (selector.isUnarySelector()) { |
Ted Kremenek | c29efd8 | 2008-05-02 17:32:38 +0000 | [diff] [blame] | 1222 | OS << selector.getIdentifierInfoForSlot(0)->getName(); |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 1223 | } else { |
| 1224 | for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) { |
Ted Kremenek | c29efd8 | 2008-05-02 17:32:38 +0000 | [diff] [blame] | 1225 | if (i < selector.getNumArgs()) { |
| 1226 | if (i > 0) OS << ' '; |
| 1227 | if (selector.getIdentifierInfoForSlot(i)) |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 1228 | OS << selector.getIdentifierInfoForSlot(i)->getName() << ':'; |
Ted Kremenek | c29efd8 | 2008-05-02 17:32:38 +0000 | [diff] [blame] | 1229 | else |
| 1230 | OS << ":"; |
| 1231 | } |
| 1232 | else OS << ", "; // Handle variadic methods. |
| 1233 | |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 1234 | PrintExpr(Mess->getArg(i)); |
| 1235 | } |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1236 | } |
| 1237 | OS << "]"; |
| 1238 | } |
| 1239 | |
Douglas Gregor | cd9b46e | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 1240 | void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) { |
| 1241 | OS << "super"; |
| 1242 | } |
| 1243 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1244 | void StmtPrinter::VisitBlockExpr(BlockExpr *Node) { |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 1245 | BlockDecl *BD = Node->getBlockDecl(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1246 | OS << "^"; |
| 1247 | |
| 1248 | const FunctionType *AFT = Node->getFunctionType(); |
| 1249 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1250 | if (isa<FunctionNoProtoType>(AFT)) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1251 | OS << "()"; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1252 | } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1253 | OS << '('; |
| 1254 | std::string ParamStr; |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 1255 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 1256 | E = BD->param_end(); AI != E; ++AI) { |
| 1257 | if (AI != BD->param_begin()) OS << ", "; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 1258 | ParamStr = (*AI)->getNameAsString(); |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1259 | (*AI)->getType().getAsStringInternal(ParamStr, Policy); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1260 | OS << ParamStr; |
| 1261 | } |
| 1262 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1263 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1264 | if (FT->isVariadic()) { |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 1265 | if (!BD->param_empty()) OS << ", "; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1266 | OS << "..."; |
| 1267 | } |
| 1268 | OS << ')'; |
| 1269 | } |
| 1270 | } |
| 1271 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1272 | void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) { |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 1273 | OS << Node->getDecl()->getNameAsString(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1274 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1275 | //===----------------------------------------------------------------------===// |
| 1276 | // Stmt method implementations |
| 1277 | //===----------------------------------------------------------------------===// |
| 1278 | |
Eli Friedman | 48d14a2 | 2009-05-30 05:03:24 +0000 | [diff] [blame] | 1279 | void Stmt::dumpPretty(ASTContext& Context) const { |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1280 | printPretty(llvm::errs(), Context, 0, |
| 1281 | PrintingPolicy(Context.getLangOptions())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
Eli Friedman | 48d14a2 | 2009-05-30 05:03:24 +0000 | [diff] [blame] | 1284 | void Stmt::printPretty(llvm::raw_ostream &OS, ASTContext& Context, |
| 1285 | PrinterHelper* Helper, |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1286 | const PrintingPolicy &Policy, |
| 1287 | unsigned Indentation) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1288 | if (this == 0) { |
| 1289 | OS << "<NULL>"; |
| 1290 | return; |
| 1291 | } |
| 1292 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1293 | if (Policy.Dump) { |
Argyrios Kyrtzidis | ad42f06 | 2009-07-14 03:20:38 +0000 | [diff] [blame] | 1294 | dump(Context.getSourceManager()); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1295 | return; |
| 1296 | } |
| 1297 | |
Eli Friedman | 48d14a2 | 2009-05-30 05:03:24 +0000 | [diff] [blame] | 1298 | StmtPrinter P(OS, Context, Helper, Policy, Indentation); |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 1299 | P.Visit(const_cast<Stmt*>(this)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1300 | } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1301 | |
| 1302 | //===----------------------------------------------------------------------===// |
| 1303 | // PrinterHelper |
| 1304 | //===----------------------------------------------------------------------===// |
| 1305 | |
| 1306 | // Implement virtual destructor. |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 1307 | PrinterHelper::~PrinterHelper() {} |