Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 1 | //===--- StmtDumper.cpp - Dumping implementation for Stmt ASTs ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Stmt::dump/Stmt::print methods, which dump out the |
| 11 | // AST in a form that exposes type details and other fields. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/AST/StmtVisitor.h" |
| 16 | #include "clang/AST/Decl.h" |
| 17 | #include "clang/AST/ExprCXX.h" |
| 18 | #include "clang/Lex/IdentifierTable.h" |
| 19 | #include "llvm/Support/Compiler.h" |
| 20 | #include <cstdio> |
| 21 | using namespace clang; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // StmtDumper Visitor |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
| 27 | namespace { |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 28 | class VISIBILITY_HIDDEN StmtDumper : public StmtVisitor<StmtDumper> { |
Chris Lattner | 0c727a3 | 2007-08-30 00:40:08 +0000 | [diff] [blame^] | 29 | const SourceManager *SM; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 30 | FILE *F; |
| 31 | unsigned IndentLevel; |
| 32 | |
| 33 | /// MaxDepth - When doing a normal dump (not dumpAll) we only want to dump |
| 34 | /// the first few levels of an AST. This keeps track of how many ast levels |
| 35 | /// are left. |
| 36 | unsigned MaxDepth; |
| 37 | public: |
Chris Lattner | 0c727a3 | 2007-08-30 00:40:08 +0000 | [diff] [blame^] | 38 | StmtDumper(const SourceManager *sm, FILE *f, unsigned maxDepth) |
| 39 | : SM(sm), F(f), IndentLevel(0), MaxDepth(maxDepth) {} |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 40 | |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 41 | void DumpSubTree(Stmt *S) { |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 42 | // Prune the recursion if not using dump all. |
| 43 | if (MaxDepth == 0) return; |
| 44 | |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 45 | ++IndentLevel; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 46 | if (S) { |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 47 | Visit(S); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 48 | } else { |
| 49 | Indent(); |
Chris Lattner | 4a70adb | 2007-08-26 03:53:29 +0000 | [diff] [blame] | 50 | fprintf(F, "<<<NULL>>>"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 51 | } |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 52 | --IndentLevel; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 55 | void DumpDeclarator(Decl *D); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 56 | |
| 57 | void Indent() const { |
| 58 | for (int i = 0, e = IndentLevel; i < e; ++i) |
| 59 | fprintf(F, " "); |
| 60 | } |
| 61 | |
Chris Lattner | fd8f7da | 2007-08-09 00:36:22 +0000 | [diff] [blame] | 62 | void DumpType(QualType T) const { |
| 63 | fprintf(F, "'%s'", T.getAsString().c_str()); |
| 64 | |
| 65 | // If the type is directly a typedef, strip off typedefness to give at |
| 66 | // least one level of concreteness. |
| 67 | if (TypedefType *TDT = dyn_cast<TypedefType>(T)) |
| 68 | fprintf(F, ":'%s'", TDT->LookThroughTypedefs().getAsString().c_str()); |
| 69 | } |
| 70 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 71 | void DumpStmt(const Stmt *Node) const { |
| 72 | Indent(); |
| 73 | fprintf(F, "(%s %p", Node->getStmtClassName(), (void*)Node); |
| 74 | } |
| 75 | |
| 76 | void DumpExpr(Expr *Node) const { |
| 77 | DumpStmt(Node); |
Chris Lattner | fd8f7da | 2007-08-09 00:36:22 +0000 | [diff] [blame] | 78 | fprintf(F, " "); |
| 79 | DumpType(Node->getType()); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 82 | void VisitStmt(Stmt *Node); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 83 | #define STMT(N, CLASS, PARENT) \ |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 84 | void Visit##CLASS(CLASS *Node); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 85 | #include "clang/AST/StmtNodes.def" |
| 86 | }; |
| 87 | } |
| 88 | |
| 89 | //===----------------------------------------------------------------------===// |
| 90 | // Stmt printing methods. |
| 91 | //===----------------------------------------------------------------------===// |
| 92 | |
| 93 | void StmtDumper::VisitStmt(Stmt *Node) { |
| 94 | Indent(); |
| 95 | fprintf(F, "<<unknown stmt type>>\n"); |
| 96 | } |
| 97 | |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 98 | void StmtDumper::DumpDeclarator(Decl *D) { |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 99 | // FIXME: Need to complete/beautify this... this code simply shows the |
| 100 | // nodes are where they need to be. |
| 101 | if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) { |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 102 | fprintf(F, "\"typedef %s %s\"", |
| 103 | localType->getUnderlyingType().getAsString().c_str(), |
| 104 | localType->getName()); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 105 | } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 106 | fprintf(F, "\""); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 107 | // Emit storage class for vardecls. |
| 108 | if (VarDecl *V = dyn_cast<VarDecl>(VD)) { |
| 109 | switch (V->getStorageClass()) { |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 110 | default: assert(0 && "Unknown storage class!"); |
| 111 | case VarDecl::None: break; |
| 112 | case VarDecl::Extern: fprintf(F, "extern "); break; |
| 113 | case VarDecl::Static: fprintf(F, "static "); break; |
| 114 | case VarDecl::Auto: fprintf(F, "auto "); break; |
| 115 | case VarDecl::Register: fprintf(F, "register "); break; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | std::string Name = VD->getName(); |
| 120 | VD->getType().getAsStringInternal(Name); |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 121 | fprintf(F, "%s", Name.c_str()); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 122 | |
| 123 | // If this is a vardecl with an initializer, emit it. |
| 124 | if (VarDecl *V = dyn_cast<VarDecl>(VD)) { |
| 125 | if (V->getInit()) { |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 126 | fprintf(F, " =\n"); |
| 127 | DumpSubTree(V->getInit()); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 130 | fprintf(F, "\""); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 131 | } else { |
| 132 | // FIXME: "struct x;" |
| 133 | assert(0 && "Unexpected decl"); |
| 134 | } |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | |
| 138 | void StmtDumper::VisitNullStmt(NullStmt *Node) { |
| 139 | DumpStmt(Node); |
| 140 | fprintf(F, ")"); |
| 141 | } |
| 142 | |
| 143 | void StmtDumper::VisitDeclStmt(DeclStmt *Node) { |
| 144 | DumpStmt(Node); |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 145 | fprintf(F, "\n"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 146 | for (Decl *D = Node->getDecl(); D; D = D->getNextDeclarator()) { |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 147 | ++IndentLevel; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 148 | Indent(); |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 149 | fprintf(F, "%p ", (void*)D); |
| 150 | DumpDeclarator(D); |
| 151 | if (D->getNextDeclarator()) |
| 152 | fprintf(F, "\n"); |
| 153 | --IndentLevel; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 154 | } |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 155 | |
| 156 | fprintf(F, ")"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void StmtDumper::VisitCompoundStmt(CompoundStmt *Node) { |
| 160 | DumpStmt(Node); |
| 161 | if (!Node->body_empty()) fprintf(F, "\n"); |
| 162 | |
| 163 | for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end(); |
| 164 | I != E; ) { |
| 165 | DumpSubTree(*I); |
| 166 | ++I; |
| 167 | if (I != E) |
| 168 | fprintf(F, "\n"); |
| 169 | } |
| 170 | fprintf(F, ")"); |
| 171 | } |
| 172 | |
| 173 | void StmtDumper::VisitCaseStmt(CaseStmt *Node) { |
Chris Lattner | a0df31a | 2007-08-09 01:04:32 +0000 | [diff] [blame] | 174 | DumpStmt(Node); |
| 175 | fprintf(F, "\n"); |
| 176 | DumpSubTree(Node->getLHS()); |
| 177 | fprintf(F, "\n"); |
| 178 | DumpSubTree(Node->getRHS()); |
| 179 | fprintf(F, "\n"); |
| 180 | DumpSubTree(Node->getSubStmt()); |
| 181 | fprintf(F, ")"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void StmtDumper::VisitDefaultStmt(DefaultStmt *Node) { |
| 185 | DumpStmt(Node); |
| 186 | fprintf(F, "\n"); |
| 187 | DumpSubTree(Node->getSubStmt()); |
| 188 | fprintf(F, ")"); |
| 189 | } |
| 190 | |
| 191 | void StmtDumper::VisitLabelStmt(LabelStmt *Node) { |
| 192 | DumpStmt(Node); |
| 193 | fprintf(F, " '%s'\n", Node->getName()); |
| 194 | DumpSubTree(Node->getSubStmt()); |
| 195 | fprintf(F, "\n"); |
| 196 | } |
| 197 | |
| 198 | void StmtDumper::VisitIfStmt(IfStmt *Node) { |
| 199 | DumpStmt(Node); |
| 200 | fprintf(F, "\n"); |
| 201 | DumpSubTree(Node->getCond()); |
| 202 | fprintf(F, "\n"); |
| 203 | DumpSubTree(Node->getThen()); |
| 204 | fprintf(F, "\n"); |
| 205 | DumpSubTree(Node->getElse()); |
| 206 | fprintf(F, ")"); |
| 207 | } |
| 208 | |
| 209 | void StmtDumper::VisitSwitchStmt(SwitchStmt *Node) { |
| 210 | DumpStmt(Node); |
| 211 | fprintf(F, "\n"); |
| 212 | DumpSubTree(Node->getCond()); |
| 213 | fprintf(F, "\n"); |
| 214 | DumpSubTree(Node->getBody()); |
| 215 | fprintf(F, ")"); |
| 216 | } |
| 217 | |
| 218 | void StmtDumper::VisitSwitchCase(SwitchCase*) { |
| 219 | assert(0 && "SwitchCase is an abstract class"); |
| 220 | } |
| 221 | |
| 222 | void StmtDumper::VisitWhileStmt(WhileStmt *Node) { |
| 223 | DumpStmt(Node); |
| 224 | fprintf(F, "\n"); |
| 225 | DumpSubTree(Node->getCond()); |
| 226 | fprintf(F, "\n"); |
| 227 | DumpSubTree(Node->getBody()); |
| 228 | fprintf(F, ")"); |
| 229 | } |
| 230 | |
| 231 | void StmtDumper::VisitDoStmt(DoStmt *Node) { |
| 232 | DumpStmt(Node); |
| 233 | fprintf(F, "\n"); |
| 234 | DumpSubTree(Node->getBody()); |
| 235 | fprintf(F, "\n"); |
| 236 | DumpSubTree(Node->getCond()); |
| 237 | fprintf(F, ")"); |
| 238 | } |
| 239 | |
| 240 | void StmtDumper::VisitForStmt(ForStmt *Node) { |
| 241 | DumpStmt(Node); |
| 242 | fprintf(F, "\n"); |
| 243 | DumpSubTree(Node->getInit()); |
| 244 | fprintf(F, "\n"); |
| 245 | DumpSubTree(Node->getCond()); |
| 246 | fprintf(F, "\n"); |
| 247 | DumpSubTree(Node->getInc()); |
| 248 | fprintf(F, "\n"); |
| 249 | DumpSubTree(Node->getBody()); |
| 250 | fprintf(F, ")"); |
| 251 | } |
| 252 | |
| 253 | void StmtDumper::VisitGotoStmt(GotoStmt *Node) { |
| 254 | DumpStmt(Node); |
| 255 | fprintf(F, " '%s':%p)", Node->getLabel()->getName(), (void*)Node->getLabel()); |
| 256 | } |
| 257 | |
| 258 | void StmtDumper::VisitIndirectGotoStmt(IndirectGotoStmt *Node) { |
| 259 | DumpStmt(Node); |
| 260 | fprintf(F, "\n"); |
| 261 | DumpSubTree(Node->getTarget()); |
| 262 | fprintf(F, ")"); |
| 263 | } |
| 264 | |
| 265 | void StmtDumper::VisitContinueStmt(ContinueStmt *Node) { |
| 266 | DumpStmt(Node); |
| 267 | fprintf(F, ")"); |
| 268 | } |
| 269 | |
| 270 | void StmtDumper::VisitBreakStmt(BreakStmt *Node) { |
| 271 | DumpStmt(Node); |
| 272 | fprintf(F, ")"); |
| 273 | } |
| 274 | |
| 275 | |
| 276 | void StmtDumper::VisitReturnStmt(ReturnStmt *Node) { |
| 277 | DumpStmt(Node); |
| 278 | if (Expr *RV = Node->getRetValue()) { |
| 279 | fprintf(F, "\n"); |
| 280 | DumpSubTree(RV); |
| 281 | } |
| 282 | fprintf(F, ")"); |
| 283 | } |
| 284 | |
| 285 | //===----------------------------------------------------------------------===// |
| 286 | // Expr printing methods. |
| 287 | //===----------------------------------------------------------------------===// |
| 288 | |
| 289 | void StmtDumper::VisitExpr(Expr *Node) { |
| 290 | DumpExpr(Node); |
| 291 | fprintf(F, ": UNKNOWN EXPR to StmtDumper)"); |
| 292 | } |
| 293 | |
| 294 | void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) { |
| 295 | DumpExpr(Node); |
| 296 | fprintf(F, " Decl='%s' %p)", Node->getDecl()->getName(), |
| 297 | (void*)Node->getDecl()); |
| 298 | } |
| 299 | |
| 300 | void StmtDumper::VisitPreDefinedExpr(PreDefinedExpr *Node) { |
| 301 | DumpExpr(Node); |
| 302 | switch (Node->getIdentType()) { |
| 303 | default: |
| 304 | assert(0 && "unknown case"); |
| 305 | case PreDefinedExpr::Func: |
| 306 | fprintf(F, " __func__)"); |
| 307 | break; |
| 308 | case PreDefinedExpr::Function: |
| 309 | fprintf(F, " __FUNCTION__)"); |
| 310 | break; |
| 311 | case PreDefinedExpr::PrettyFunction: |
| 312 | fprintf(F, " __PRETTY_FUNCTION__)"); |
| 313 | break; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) { |
Chris Lattner | a0df31a | 2007-08-09 01:04:32 +0000 | [diff] [blame] | 318 | DumpExpr(Node); |
| 319 | fprintf(F, " %d)", Node->getValue()); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) { |
| 323 | DumpExpr(Node); |
| 324 | |
| 325 | bool isSigned = Node->getType()->isSignedIntegerType(); |
| 326 | fprintf(F, " %s)", Node->getValue().toString(10, isSigned).c_str()); |
| 327 | } |
| 328 | void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) { |
| 329 | DumpExpr(Node); |
| 330 | fprintf(F, " %f)", Node->getValue()); |
| 331 | } |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 332 | |
| 333 | void StmtDumper::VisitImaginaryLiteral(ImaginaryLiteral *Node) { |
| 334 | DumpExpr(Node); |
| 335 | fprintf(F, "\n"); |
| 336 | DumpSubTree(Node->getSubExpr()); |
| 337 | fprintf(F, ")"); |
| 338 | } |
| 339 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 340 | void StmtDumper::VisitStringLiteral(StringLiteral *Str) { |
Chris Lattner | a0df31a | 2007-08-09 01:04:32 +0000 | [diff] [blame] | 341 | DumpExpr(Str); |
| 342 | // FIXME: this doesn't print wstrings right. |
Chris Lattner | 5fc6107 | 2007-08-09 17:14:24 +0000 | [diff] [blame] | 343 | fprintf(F, " %s\"", Str->isWide() ? "L" : ""); |
Chris Lattner | a0df31a | 2007-08-09 01:04:32 +0000 | [diff] [blame] | 344 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 345 | for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) { |
Chris Lattner | 5fc6107 | 2007-08-09 17:14:24 +0000 | [diff] [blame] | 346 | switch (char C = Str->getStrData()[i]) { |
| 347 | default: |
| 348 | if (isprint(C)) |
| 349 | fputc(C, F); |
| 350 | else |
| 351 | fprintf(F, "\\%03o", C); |
| 352 | break; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 353 | // Handle some common ones to make dumps prettier. |
Chris Lattner | 5fc6107 | 2007-08-09 17:14:24 +0000 | [diff] [blame] | 354 | case '\\': fprintf(F, "\\\\"); break; |
| 355 | case '"': fprintf(F, "\\\""); break; |
| 356 | case '\n': fprintf(F, "\\n"); break; |
| 357 | case '\t': fprintf(F, "\\t"); break; |
| 358 | case '\a': fprintf(F, "\\a"); break; |
| 359 | case '\b': fprintf(F, "\\b"); break; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 360 | } |
| 361 | } |
Chris Lattner | 5fc6107 | 2007-08-09 17:14:24 +0000 | [diff] [blame] | 362 | fprintf(F, "\")"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 363 | } |
| 364 | void StmtDumper::VisitParenExpr(ParenExpr *Node) { |
| 365 | DumpExpr(Node); |
| 366 | fprintf(F, "\n"); |
| 367 | DumpSubTree(Node->getSubExpr()); |
| 368 | fprintf(F, ")"); |
| 369 | } |
| 370 | void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) { |
Chris Lattner | 13cb21f | 2007-08-09 17:35:30 +0000 | [diff] [blame] | 371 | DumpExpr(Node); |
| 372 | fprintf(F, " %s '%s'\n", Node->isPostfix() ? "postfix" : "prefix", |
| 373 | UnaryOperator::getOpcodeStr(Node->getOpcode())); |
| 374 | DumpSubTree(Node->getSubExpr()); |
| 375 | fprintf(F, ")"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 376 | } |
| 377 | void StmtDumper::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) { |
Chris Lattner | 13cb21f | 2007-08-09 17:35:30 +0000 | [diff] [blame] | 378 | DumpExpr(Node); |
| 379 | fprintf(F, " %s ", Node->isSizeOf() ? "sizeof" : "alignof"); |
| 380 | DumpType(Node->getArgumentType()); |
| 381 | fprintf(F, ")"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 382 | } |
| 383 | void StmtDumper::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) { |
| 384 | DumpExpr(Node); |
| 385 | fprintf(F, "\n"); |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 386 | DumpSubTree(Node->getLHS()); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 387 | fprintf(F, "\n"); |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 388 | DumpSubTree(Node->getRHS()); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 389 | fprintf(F, ")"); |
| 390 | } |
| 391 | |
| 392 | void StmtDumper::VisitCallExpr(CallExpr *Node) { |
| 393 | DumpExpr(Node); |
| 394 | fprintf(F, "\n"); |
| 395 | DumpSubTree(Node->getCallee()); |
| 396 | |
| 397 | for (unsigned i = 0, e = Node->getNumArgs(); i != e; ++i) { |
| 398 | fprintf(F, "\n"); |
| 399 | DumpSubTree(Node->getArg(i)); |
| 400 | } |
| 401 | fprintf(F, ")"); |
| 402 | } |
Chris Lattner | 13cb21f | 2007-08-09 17:35:30 +0000 | [diff] [blame] | 403 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 404 | void StmtDumper::VisitMemberExpr(MemberExpr *Node) { |
Chris Lattner | 13cb21f | 2007-08-09 17:35:30 +0000 | [diff] [blame] | 405 | DumpExpr(Node); |
| 406 | fprintf(F, " %s%s %p\n", Node->isArrow() ? "->" : ".", |
| 407 | Node->getMemberDecl()->getName(), (void*)Node->getMemberDecl()); |
| 408 | DumpSubTree(Node->getBase()); |
| 409 | fprintf(F, ")"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 410 | } |
| 411 | void StmtDumper::VisitOCUVectorElementExpr(OCUVectorElementExpr *Node) { |
Chris Lattner | 13cb21f | 2007-08-09 17:35:30 +0000 | [diff] [blame] | 412 | DumpExpr(Node); |
| 413 | fprintf(F, " %s\n", Node->getAccessor().getName()); |
| 414 | DumpSubTree(Node->getBase()); |
| 415 | fprintf(F, ")"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 416 | } |
| 417 | void StmtDumper::VisitCastExpr(CastExpr *Node) { |
Chris Lattner | 13cb21f | 2007-08-09 17:35:30 +0000 | [diff] [blame] | 418 | DumpExpr(Node); |
| 419 | fprintf(F, "\n"); |
| 420 | DumpSubTree(Node->getSubExpr()); |
| 421 | fprintf(F, ")"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 422 | } |
| 423 | void StmtDumper::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) { |
Chris Lattner | 13cb21f | 2007-08-09 17:35:30 +0000 | [diff] [blame] | 424 | DumpExpr(Node); |
| 425 | fprintf(F, "\n"); |
| 426 | DumpSubTree(Node->getInitializer()); |
| 427 | fprintf(F, ")"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 428 | } |
| 429 | void StmtDumper::VisitImplicitCastExpr(ImplicitCastExpr *Node) { |
| 430 | DumpExpr(Node); |
| 431 | fprintf(F, "\n"); |
| 432 | DumpSubTree(Node->getSubExpr()); |
| 433 | fprintf(F, ")"); |
| 434 | } |
| 435 | void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) { |
| 436 | DumpExpr(Node); |
Chris Lattner | eb14fe8 | 2007-08-25 02:00:02 +0000 | [diff] [blame] | 437 | fprintf(F, " '%s'\n", BinaryOperator::getOpcodeStr(Node->getOpcode())); |
| 438 | DumpSubTree(Node->getLHS()); |
| 439 | fprintf(F, "\n"); |
| 440 | DumpSubTree(Node->getRHS()); |
| 441 | fprintf(F, ")"); |
| 442 | } |
| 443 | void StmtDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) { |
| 444 | DumpExpr(Node); |
| 445 | fprintf(F, " '%s' ComputeTy=", |
| 446 | BinaryOperator::getOpcodeStr(Node->getOpcode())); |
| 447 | DumpType(Node->getComputationType()); |
Chris Lattner | dfce2a5 | 2007-08-24 16:24:49 +0000 | [diff] [blame] | 448 | fprintf(F, "\n"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 449 | DumpSubTree(Node->getLHS()); |
| 450 | fprintf(F, "\n"); |
| 451 | DumpSubTree(Node->getRHS()); |
| 452 | fprintf(F, ")"); |
| 453 | } |
| 454 | void StmtDumper::VisitConditionalOperator(ConditionalOperator *Node) { |
| 455 | DumpExpr(Node); |
| 456 | fprintf(F, "\n"); |
| 457 | DumpSubTree(Node->getCond()); |
| 458 | fprintf(F, "\n"); |
| 459 | DumpSubTree(Node->getLHS()); |
| 460 | fprintf(F, "\n"); |
| 461 | DumpSubTree(Node->getRHS()); |
| 462 | fprintf(F, ")"); |
| 463 | } |
| 464 | |
| 465 | // GNU extensions. |
| 466 | |
| 467 | void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) { |
Chris Lattner | 13cb21f | 2007-08-09 17:35:30 +0000 | [diff] [blame] | 468 | DumpExpr(Node); |
| 469 | fprintf(F, " %s %p)", Node->getLabel()->getName(), (void*)Node->getLabel()); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Chris Lattner | 13cb21f | 2007-08-09 17:35:30 +0000 | [diff] [blame] | 472 | void StmtDumper::VisitStmtExpr(StmtExpr *Node) { |
| 473 | DumpExpr(Node); |
| 474 | fprintf(F, "\n"); |
| 475 | DumpSubTree(Node->getSubStmt()); |
| 476 | fprintf(F, ")"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | void StmtDumper::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) { |
Chris Lattner | 13cb21f | 2007-08-09 17:35:30 +0000 | [diff] [blame] | 480 | DumpExpr(Node); |
| 481 | fprintf(F, " "); |
| 482 | DumpType(Node->getArgType1()); |
| 483 | fprintf(F, " "); |
| 484 | DumpType(Node->getArgType2()); |
| 485 | fprintf(F, ")"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | void StmtDumper::VisitChooseExpr(ChooseExpr *Node) { |
Chris Lattner | 13cb21f | 2007-08-09 17:35:30 +0000 | [diff] [blame] | 489 | DumpExpr(Node); |
| 490 | fprintf(F, "\n"); |
| 491 | DumpSubTree(Node->getCond()); |
| 492 | fprintf(F, "\n"); |
| 493 | DumpSubTree(Node->getLHS()); |
| 494 | fprintf(F, "\n"); |
| 495 | DumpSubTree(Node->getRHS()); |
| 496 | fprintf(F, ")"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 499 | //===----------------------------------------------------------------------===// |
| 500 | // C++ Expressions |
| 501 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 502 | |
| 503 | void StmtDumper::VisitCXXCastExpr(CXXCastExpr *Node) { |
Chris Lattner | 13cb21f | 2007-08-09 17:35:30 +0000 | [diff] [blame] | 504 | DumpExpr(Node); |
| 505 | fprintf(F, " %s\n", CXXCastExpr::getOpcodeStr(Node->getOpcode())); |
| 506 | DumpSubTree(Node->getSubExpr()); |
| 507 | fprintf(F, ")"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { |
Chris Lattner | 13cb21f | 2007-08-09 17:35:30 +0000 | [diff] [blame] | 511 | DumpExpr(Node); |
| 512 | fprintf(F, " %s)", Node->getValue() ? "true" : "false"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 515 | //===----------------------------------------------------------------------===// |
| 516 | // Obj-C Expressions |
| 517 | //===----------------------------------------------------------------------===// |
| 518 | |
| 519 | void StmtDumper::VisitObjCStringLiteral(ObjCStringLiteral *Node) { |
| 520 | DumpExpr(Node); |
| 521 | fprintf(F, "\n"); |
| 522 | DumpSubTree(Node->getString()); |
| 523 | fprintf(F, ")"); |
| 524 | } |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 525 | |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 526 | void StmtDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { |
| 527 | DumpExpr(Node); |
| 528 | |
| 529 | fprintf(F, " "); |
| 530 | DumpType(Node->getEncodedType()); |
| 531 | fprintf(F, ")"); |
| 532 | } |
| 533 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 534 | //===----------------------------------------------------------------------===// |
| 535 | // Stmt method implementations |
| 536 | //===----------------------------------------------------------------------===// |
| 537 | |
| 538 | /// dump - This does a local dump of the specified AST fragment. It dumps the |
| 539 | /// specified node and a few nodes underneath it, but not the whole subtree. |
| 540 | /// This is useful in a debugger. |
Chris Lattner | 0c727a3 | 2007-08-30 00:40:08 +0000 | [diff] [blame^] | 541 | void Stmt::dump(const SourceManager &SM) const { |
| 542 | StmtDumper P(&SM, stderr, 4); |
| 543 | P.Visit(const_cast<Stmt*>(this)); |
| 544 | fprintf(stderr, "\n"); |
| 545 | } |
| 546 | |
| 547 | /// dump - This does a local dump of the specified AST fragment. It dumps the |
| 548 | /// specified node and a few nodes underneath it, but not the whole subtree. |
| 549 | /// This is useful in a debugger. |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 550 | void Stmt::dump() const { |
Chris Lattner | 0c727a3 | 2007-08-30 00:40:08 +0000 | [diff] [blame^] | 551 | StmtDumper P(0, stderr, 4); |
| 552 | P.Visit(const_cast<Stmt*>(this)); |
| 553 | fprintf(stderr, "\n"); |
| 554 | } |
| 555 | |
| 556 | /// dumpAll - This does a dump of the specified AST fragment and all subtrees. |
| 557 | void Stmt::dumpAll(const SourceManager &SM) const { |
| 558 | StmtDumper P(&SM, stderr, ~0U); |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 559 | P.Visit(const_cast<Stmt*>(this)); |
Chris Lattner | ee41ce5 | 2007-08-10 21:51:12 +0000 | [diff] [blame] | 560 | fprintf(stderr, "\n"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | /// dumpAll - This does a dump of the specified AST fragment and all subtrees. |
| 564 | void Stmt::dumpAll() const { |
Chris Lattner | 0c727a3 | 2007-08-30 00:40:08 +0000 | [diff] [blame^] | 565 | StmtDumper P(0, stderr, ~0U); |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 566 | P.Visit(const_cast<Stmt*>(this)); |
Chris Lattner | ee41ce5 | 2007-08-10 21:51:12 +0000 | [diff] [blame] | 567 | fprintf(stderr, "\n"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 568 | } |