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