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