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