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" |
Chris Lattner | c7229c3 | 2007-10-07 08:58:51 +0000 | [diff] [blame] | 18 | #include "clang/Basic/IdentifierTable.h" |
Chris Lattner | e300c87 | 2007-08-30 06:17:34 +0000 | [diff] [blame] | 19 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compiler.h" |
| 21 | #include <cstdio> |
| 22 | using namespace clang; |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | // StmtDumper Visitor |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | namespace { |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 29 | class VISIBILITY_HIDDEN StmtDumper : public StmtVisitor<StmtDumper> { |
Chris Lattner | e300c87 | 2007-08-30 06:17:34 +0000 | [diff] [blame] | 30 | SourceManager *SM; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 31 | FILE *F; |
| 32 | unsigned IndentLevel; |
| 33 | |
| 34 | /// MaxDepth - When doing a normal dump (not dumpAll) we only want to dump |
| 35 | /// the first few levels of an AST. This keeps track of how many ast levels |
| 36 | /// are left. |
| 37 | unsigned MaxDepth; |
Chris Lattner | e300c87 | 2007-08-30 06:17:34 +0000 | [diff] [blame] | 38 | |
| 39 | /// LastLocFilename/LastLocLine - Keep track of the last location we print |
| 40 | /// out so that we can print out deltas from then on out. |
| 41 | const char *LastLocFilename; |
| 42 | unsigned LastLocLine; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 43 | public: |
Chris Lattner | e300c87 | 2007-08-30 06:17:34 +0000 | [diff] [blame] | 44 | StmtDumper(SourceManager *sm, FILE *f, unsigned maxDepth) |
| 45 | : SM(sm), F(f), IndentLevel(0-1), MaxDepth(maxDepth) { |
| 46 | LastLocFilename = ""; |
| 47 | LastLocLine = ~0U; |
| 48 | } |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 49 | |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 50 | void DumpSubTree(Stmt *S) { |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 51 | // Prune the recursion if not using dump all. |
| 52 | if (MaxDepth == 0) return; |
| 53 | |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 54 | ++IndentLevel; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 55 | if (S) { |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 56 | Visit(S); |
Chris Lattner | b393879 | 2007-08-30 00:53:54 +0000 | [diff] [blame] | 57 | |
| 58 | // Print out children. |
| 59 | Stmt::child_iterator CI = S->child_begin(), CE = S->child_end(); |
| 60 | if (CI != CE) { |
| 61 | while (CI != CE) { |
| 62 | fprintf(F, "\n"); |
| 63 | DumpSubTree(*CI++); |
| 64 | } |
| 65 | } |
| 66 | fprintf(F, ")"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 67 | } else { |
| 68 | Indent(); |
Chris Lattner | 4a70adb | 2007-08-26 03:53:29 +0000 | [diff] [blame] | 69 | fprintf(F, "<<<NULL>>>"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 70 | } |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 71 | --IndentLevel; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 74 | void DumpDeclarator(Decl *D); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 75 | |
| 76 | void Indent() const { |
| 77 | for (int i = 0, e = IndentLevel; i < e; ++i) |
| 78 | fprintf(F, " "); |
| 79 | } |
| 80 | |
Steve Naroff | 9dcbfa4 | 2007-09-01 21:08:38 +0000 | [diff] [blame] | 81 | void DumpType(QualType T) { |
Chris Lattner | fd8f7da | 2007-08-09 00:36:22 +0000 | [diff] [blame] | 82 | fprintf(F, "'%s'", T.getAsString().c_str()); |
| 83 | |
| 84 | // If the type is directly a typedef, strip off typedefness to give at |
| 85 | // least one level of concreteness. |
| 86 | if (TypedefType *TDT = dyn_cast<TypedefType>(T)) |
| 87 | fprintf(F, ":'%s'", TDT->LookThroughTypedefs().getAsString().c_str()); |
| 88 | } |
Steve Naroff | 9dcbfa4 | 2007-09-01 21:08:38 +0000 | [diff] [blame] | 89 | void DumpStmt(const Stmt *Node) { |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 90 | Indent(); |
| 91 | fprintf(F, "(%s %p", Node->getStmtClassName(), (void*)Node); |
Steve Naroff | 9dcbfa4 | 2007-09-01 21:08:38 +0000 | [diff] [blame] | 92 | DumpSourceRange(Node); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 93 | } |
Steve Naroff | 9dcbfa4 | 2007-09-01 21:08:38 +0000 | [diff] [blame] | 94 | void DumpExpr(const Expr *Node) { |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 95 | DumpStmt(Node); |
Chris Lattner | fd8f7da | 2007-08-09 00:36:22 +0000 | [diff] [blame] | 96 | fprintf(F, " "); |
| 97 | DumpType(Node->getType()); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 98 | } |
Steve Naroff | 9dcbfa4 | 2007-09-01 21:08:38 +0000 | [diff] [blame] | 99 | void DumpSourceRange(const Stmt *Node); |
Chris Lattner | e300c87 | 2007-08-30 06:17:34 +0000 | [diff] [blame] | 100 | void DumpLocation(SourceLocation Loc); |
Chris Lattner | e300c87 | 2007-08-30 06:17:34 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 17a1a72 | 2007-08-30 01:00:35 +0000 | [diff] [blame] | 102 | // Stmts. |
Chris Lattner | c5598cb | 2007-08-21 04:04:25 +0000 | [diff] [blame] | 103 | void VisitStmt(Stmt *Node); |
Chris Lattner | 17a1a72 | 2007-08-30 01:00:35 +0000 | [diff] [blame] | 104 | void VisitDeclStmt(DeclStmt *Node); |
| 105 | void VisitLabelStmt(LabelStmt *Node); |
| 106 | void VisitGotoStmt(GotoStmt *Node); |
| 107 | |
| 108 | // Exprs |
| 109 | void VisitExpr(Expr *Node); |
| 110 | void VisitDeclRefExpr(DeclRefExpr *Node); |
| 111 | void VisitPreDefinedExpr(PreDefinedExpr *Node); |
| 112 | void VisitCharacterLiteral(CharacterLiteral *Node); |
| 113 | void VisitIntegerLiteral(IntegerLiteral *Node); |
| 114 | void VisitFloatingLiteral(FloatingLiteral *Node); |
| 115 | void VisitStringLiteral(StringLiteral *Str); |
| 116 | void VisitUnaryOperator(UnaryOperator *Node); |
| 117 | void VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node); |
| 118 | void VisitMemberExpr(MemberExpr *Node); |
| 119 | void VisitOCUVectorElementExpr(OCUVectorElementExpr *Node); |
| 120 | void VisitBinaryOperator(BinaryOperator *Node); |
| 121 | void VisitCompoundAssignOperator(CompoundAssignOperator *Node); |
| 122 | void VisitAddrLabelExpr(AddrLabelExpr *Node); |
| 123 | void VisitTypesCompatibleExpr(TypesCompatibleExpr *Node); |
| 124 | |
| 125 | // C++ |
| 126 | void VisitCXXCastExpr(CXXCastExpr *Node); |
| 127 | void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node); |
| 128 | |
| 129 | // ObjC |
| 130 | void VisitObjCEncodeExpr(ObjCEncodeExpr *Node); |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame^] | 131 | void VisitObjCSelectorExpr(ObjCSelectorExpr *Node); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 132 | }; |
| 133 | } |
| 134 | |
| 135 | //===----------------------------------------------------------------------===// |
Chris Lattner | e300c87 | 2007-08-30 06:17:34 +0000 | [diff] [blame] | 136 | // Utilities |
| 137 | //===----------------------------------------------------------------------===// |
| 138 | |
| 139 | void StmtDumper::DumpLocation(SourceLocation Loc) { |
| 140 | SourceLocation PhysLoc = SM->getPhysicalLoc(Loc); |
| 141 | |
| 142 | // The general format we print out is filename:line:col, but we drop pieces |
| 143 | // that haven't changed since the last loc printed. |
| 144 | const char *Filename = SM->getSourceName(PhysLoc); |
| 145 | unsigned LineNo = SM->getLineNumber(PhysLoc); |
| 146 | if (strcmp(Filename, LastLocFilename) != 0) { |
| 147 | fprintf(stderr, "%s:%u:%u", Filename, LineNo, SM->getColumnNumber(PhysLoc)); |
| 148 | LastLocFilename = Filename; |
| 149 | LastLocLine = LineNo; |
| 150 | } else if (LineNo != LastLocLine) { |
| 151 | fprintf(stderr, "line:%u:%u", LineNo, SM->getColumnNumber(PhysLoc)); |
| 152 | LastLocLine = LineNo; |
| 153 | } else { |
| 154 | fprintf(stderr, "col:%u", SM->getColumnNumber(PhysLoc)); |
| 155 | } |
| 156 | } |
| 157 | |
Steve Naroff | 9dcbfa4 | 2007-09-01 21:08:38 +0000 | [diff] [blame] | 158 | void StmtDumper::DumpSourceRange(const Stmt *Node) { |
Chris Lattner | e300c87 | 2007-08-30 06:17:34 +0000 | [diff] [blame] | 159 | // Can't translate locations if a SourceManager isn't available. |
| 160 | if (SM == 0) return; |
| 161 | |
| 162 | // TODO: If the parent expression is available, we can print a delta vs its |
| 163 | // location. |
| 164 | SourceRange R = Node->getSourceRange(); |
| 165 | |
| 166 | fprintf(stderr, " <"); |
| 167 | DumpLocation(R.Begin()); |
| 168 | if (R.Begin() != R.End()) { |
| 169 | fprintf(stderr, ", "); |
| 170 | DumpLocation(R.End()); |
| 171 | } |
| 172 | fprintf(stderr, ">"); |
| 173 | |
| 174 | // <t2.c:123:421[blah], t2.c:412:321> |
| 175 | |
| 176 | } |
| 177 | |
| 178 | |
| 179 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 180 | // Stmt printing methods. |
| 181 | //===----------------------------------------------------------------------===// |
| 182 | |
| 183 | void StmtDumper::VisitStmt(Stmt *Node) { |
Chris Lattner | 17a1a72 | 2007-08-30 01:00:35 +0000 | [diff] [blame] | 184 | DumpStmt(Node); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 187 | void StmtDumper::DumpDeclarator(Decl *D) { |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 188 | // FIXME: Need to complete/beautify this... this code simply shows the |
| 189 | // nodes are where they need to be. |
| 190 | if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) { |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 191 | fprintf(F, "\"typedef %s %s\"", |
| 192 | localType->getUnderlyingType().getAsString().c_str(), |
| 193 | localType->getName()); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 194 | } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 195 | fprintf(F, "\""); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 196 | // Emit storage class for vardecls. |
| 197 | if (VarDecl *V = dyn_cast<VarDecl>(VD)) { |
| 198 | switch (V->getStorageClass()) { |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 199 | default: assert(0 && "Unknown storage class!"); |
| 200 | case VarDecl::None: break; |
| 201 | case VarDecl::Extern: fprintf(F, "extern "); break; |
| 202 | case VarDecl::Static: fprintf(F, "static "); break; |
| 203 | case VarDecl::Auto: fprintf(F, "auto "); break; |
| 204 | case VarDecl::Register: fprintf(F, "register "); break; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
| 208 | std::string Name = VD->getName(); |
| 209 | VD->getType().getAsStringInternal(Name); |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 210 | fprintf(F, "%s", Name.c_str()); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 211 | |
| 212 | // If this is a vardecl with an initializer, emit it. |
| 213 | if (VarDecl *V = dyn_cast<VarDecl>(VD)) { |
| 214 | if (V->getInit()) { |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 215 | fprintf(F, " =\n"); |
| 216 | DumpSubTree(V->getInit()); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 219 | fprintf(F, "\""); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 220 | } else { |
| 221 | // FIXME: "struct x;" |
| 222 | assert(0 && "Unexpected decl"); |
| 223 | } |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 226 | void StmtDumper::VisitDeclStmt(DeclStmt *Node) { |
| 227 | DumpStmt(Node); |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 228 | fprintf(F, "\n"); |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 229 | for (ScopedDecl *D = Node->getDecl(); D; D = D->getNextDeclarator()) { |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 230 | ++IndentLevel; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 231 | Indent(); |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 232 | fprintf(F, "%p ", (void*)D); |
| 233 | DumpDeclarator(D); |
| 234 | if (D->getNextDeclarator()) |
| 235 | fprintf(F, "\n"); |
| 236 | --IndentLevel; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 237 | } |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 240 | void StmtDumper::VisitLabelStmt(LabelStmt *Node) { |
| 241 | DumpStmt(Node); |
| 242 | fprintf(F, " '%s'\n", Node->getName()); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 245 | void StmtDumper::VisitGotoStmt(GotoStmt *Node) { |
| 246 | DumpStmt(Node); |
Chris Lattner | b393879 | 2007-08-30 00:53:54 +0000 | [diff] [blame] | 247 | fprintf(F, " '%s':%p", Node->getLabel()->getName(), (void*)Node->getLabel()); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 250 | //===----------------------------------------------------------------------===// |
| 251 | // Expr printing methods. |
| 252 | //===----------------------------------------------------------------------===// |
| 253 | |
| 254 | void StmtDumper::VisitExpr(Expr *Node) { |
| 255 | DumpExpr(Node); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) { |
| 259 | DumpExpr(Node); |
Ted Kremenek | eb641f9 | 2007-09-10 17:32:55 +0000 | [diff] [blame] | 260 | |
| 261 | fprintf(F, " "); |
| 262 | switch (Node->getDecl()->getKind()) { |
| 263 | case Decl::Function: fprintf(F,"FunctionDecl"); break; |
Chris Lattner | aa9fc46 | 2007-10-08 21:37:32 +0000 | [diff] [blame] | 264 | case Decl::BlockVar: fprintf(F,"BlockVar"); break; |
| 265 | case Decl::FileVar: fprintf(F,"FileVar"); break; |
| 266 | case Decl::ParmVar: fprintf(F,"ParmVar"); break; |
Ted Kremenek | eb641f9 | 2007-09-10 17:32:55 +0000 | [diff] [blame] | 267 | case Decl::EnumConstant: fprintf(F,"EnumConstant"); break; |
| 268 | case Decl::Typedef: fprintf(F,"Typedef"); break; |
| 269 | case Decl::Struct: fprintf(F,"Struct"); break; |
| 270 | case Decl::Union: fprintf(F,"Union"); break; |
| 271 | case Decl::Class: fprintf(F,"Class"); break; |
| 272 | case Decl::Enum: fprintf(F,"Enum"); break; |
| 273 | case Decl::ObjcInterface: fprintf(F,"ObjcInterface"); break; |
| 274 | case Decl::ObjcClass: fprintf(F,"ObjcClass"); break; |
| 275 | default: fprintf(F,"Decl"); break; |
| 276 | } |
| 277 | |
| 278 | fprintf(F, "='%s' %p", Node->getDecl()->getName(), (void*)Node->getDecl()); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | void StmtDumper::VisitPreDefinedExpr(PreDefinedExpr *Node) { |
| 282 | DumpExpr(Node); |
| 283 | switch (Node->getIdentType()) { |
| 284 | default: |
| 285 | assert(0 && "unknown case"); |
| 286 | case PreDefinedExpr::Func: |
Chris Lattner | b393879 | 2007-08-30 00:53:54 +0000 | [diff] [blame] | 287 | fprintf(F, " __func__"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 288 | break; |
| 289 | case PreDefinedExpr::Function: |
Chris Lattner | b393879 | 2007-08-30 00:53:54 +0000 | [diff] [blame] | 290 | fprintf(F, " __FUNCTION__"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 291 | break; |
| 292 | case PreDefinedExpr::PrettyFunction: |
Chris Lattner | b393879 | 2007-08-30 00:53:54 +0000 | [diff] [blame] | 293 | fprintf(F, " __PRETTY_FUNCTION__"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 294 | break; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) { |
Chris Lattner | a0df31a | 2007-08-09 01:04:32 +0000 | [diff] [blame] | 299 | DumpExpr(Node); |
Chris Lattner | b393879 | 2007-08-30 00:53:54 +0000 | [diff] [blame] | 300 | fprintf(F, " %d", Node->getValue()); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) { |
| 304 | DumpExpr(Node); |
| 305 | |
| 306 | bool isSigned = Node->getType()->isSignedIntegerType(); |
Chris Lattner | b393879 | 2007-08-30 00:53:54 +0000 | [diff] [blame] | 307 | fprintf(F, " %s", Node->getValue().toString(10, isSigned).c_str()); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 308 | } |
| 309 | void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) { |
| 310 | DumpExpr(Node); |
Chris Lattner | c9bec4b | 2007-09-22 18:47:25 +0000 | [diff] [blame] | 311 | fprintf(F, " %f", Node->getValueAsDouble()); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 312 | } |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 313 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 314 | void StmtDumper::VisitStringLiteral(StringLiteral *Str) { |
Chris Lattner | a0df31a | 2007-08-09 01:04:32 +0000 | [diff] [blame] | 315 | DumpExpr(Str); |
| 316 | // FIXME: this doesn't print wstrings right. |
Chris Lattner | 5fc6107 | 2007-08-09 17:14:24 +0000 | [diff] [blame] | 317 | fprintf(F, " %s\"", Str->isWide() ? "L" : ""); |
Chris Lattner | a0df31a | 2007-08-09 01:04:32 +0000 | [diff] [blame] | 318 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 319 | for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) { |
Chris Lattner | 5fc6107 | 2007-08-09 17:14:24 +0000 | [diff] [blame] | 320 | switch (char C = Str->getStrData()[i]) { |
| 321 | default: |
| 322 | if (isprint(C)) |
| 323 | fputc(C, F); |
| 324 | else |
| 325 | fprintf(F, "\\%03o", C); |
| 326 | break; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 327 | // Handle some common ones to make dumps prettier. |
Chris Lattner | 5fc6107 | 2007-08-09 17:14:24 +0000 | [diff] [blame] | 328 | case '\\': fprintf(F, "\\\\"); break; |
| 329 | case '"': fprintf(F, "\\\""); break; |
| 330 | case '\n': fprintf(F, "\\n"); break; |
| 331 | case '\t': fprintf(F, "\\t"); break; |
| 332 | case '\a': fprintf(F, "\\a"); break; |
| 333 | case '\b': fprintf(F, "\\b"); break; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 334 | } |
| 335 | } |
Chris Lattner | b393879 | 2007-08-30 00:53:54 +0000 | [diff] [blame] | 336 | fprintf(F, "\""); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 337 | } |
Chris Lattner | 17a1a72 | 2007-08-30 01:00:35 +0000 | [diff] [blame] | 338 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 339 | void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) { |
Chris Lattner | 13cb21f | 2007-08-09 17:35:30 +0000 | [diff] [blame] | 340 | DumpExpr(Node); |
Chris Lattner | b393879 | 2007-08-30 00:53:54 +0000 | [diff] [blame] | 341 | fprintf(F, " %s '%s'", Node->isPostfix() ? "postfix" : "prefix", |
Chris Lattner | 13cb21f | 2007-08-09 17:35:30 +0000 | [diff] [blame] | 342 | UnaryOperator::getOpcodeStr(Node->getOpcode())); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 343 | } |
| 344 | void StmtDumper::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) { |
Chris Lattner | 13cb21f | 2007-08-09 17:35:30 +0000 | [diff] [blame] | 345 | DumpExpr(Node); |
| 346 | fprintf(F, " %s ", Node->isSizeOf() ? "sizeof" : "alignof"); |
| 347 | DumpType(Node->getArgumentType()); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 348 | } |
Chris Lattner | 13cb21f | 2007-08-09 17:35:30 +0000 | [diff] [blame] | 349 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 350 | void StmtDumper::VisitMemberExpr(MemberExpr *Node) { |
Chris Lattner | 13cb21f | 2007-08-09 17:35:30 +0000 | [diff] [blame] | 351 | DumpExpr(Node); |
Chris Lattner | b393879 | 2007-08-30 00:53:54 +0000 | [diff] [blame] | 352 | fprintf(F, " %s%s %p", Node->isArrow() ? "->" : ".", |
Chris Lattner | 13cb21f | 2007-08-09 17:35:30 +0000 | [diff] [blame] | 353 | Node->getMemberDecl()->getName(), (void*)Node->getMemberDecl()); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 354 | } |
| 355 | void StmtDumper::VisitOCUVectorElementExpr(OCUVectorElementExpr *Node) { |
Chris Lattner | 13cb21f | 2007-08-09 17:35:30 +0000 | [diff] [blame] | 356 | DumpExpr(Node); |
Chris Lattner | b393879 | 2007-08-30 00:53:54 +0000 | [diff] [blame] | 357 | fprintf(F, " %s", Node->getAccessor().getName()); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 358 | } |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 359 | void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) { |
| 360 | DumpExpr(Node); |
Chris Lattner | b393879 | 2007-08-30 00:53:54 +0000 | [diff] [blame] | 361 | fprintf(F, " '%s'", BinaryOperator::getOpcodeStr(Node->getOpcode())); |
Chris Lattner | eb14fe8 | 2007-08-25 02:00:02 +0000 | [diff] [blame] | 362 | } |
| 363 | void StmtDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) { |
| 364 | DumpExpr(Node); |
| 365 | fprintf(F, " '%s' ComputeTy=", |
| 366 | BinaryOperator::getOpcodeStr(Node->getOpcode())); |
| 367 | DumpType(Node->getComputationType()); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 368 | } |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 369 | |
| 370 | // GNU extensions. |
| 371 | |
| 372 | void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) { |
Chris Lattner | 13cb21f | 2007-08-09 17:35:30 +0000 | [diff] [blame] | 373 | DumpExpr(Node); |
Chris Lattner | b393879 | 2007-08-30 00:53:54 +0000 | [diff] [blame] | 374 | fprintf(F, " %s %p", Node->getLabel()->getName(), (void*)Node->getLabel()); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 377 | void StmtDumper::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) { |
Chris Lattner | 13cb21f | 2007-08-09 17:35:30 +0000 | [diff] [blame] | 378 | DumpExpr(Node); |
| 379 | fprintf(F, " "); |
| 380 | DumpType(Node->getArgType1()); |
| 381 | fprintf(F, " "); |
| 382 | DumpType(Node->getArgType2()); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Chris Lattner | f9e0581 | 2007-08-09 18:03:18 +0000 | [diff] [blame] | 385 | //===----------------------------------------------------------------------===// |
| 386 | // C++ Expressions |
| 387 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 388 | |
| 389 | void StmtDumper::VisitCXXCastExpr(CXXCastExpr *Node) { |
Chris Lattner | 13cb21f | 2007-08-09 17:35:30 +0000 | [diff] [blame] | 390 | DumpExpr(Node); |
Chris Lattner | b393879 | 2007-08-30 00:53:54 +0000 | [diff] [blame] | 391 | fprintf(F, " %s", CXXCastExpr::getOpcodeStr(Node->getOpcode())); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { |
Chris Lattner | 13cb21f | 2007-08-09 17:35:30 +0000 | [diff] [blame] | 395 | DumpExpr(Node); |
Chris Lattner | b393879 | 2007-08-30 00:53:54 +0000 | [diff] [blame] | 396 | fprintf(F, " %s", Node->getValue() ? "true" : "false"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 399 | //===----------------------------------------------------------------------===// |
| 400 | // Obj-C Expressions |
| 401 | //===----------------------------------------------------------------------===// |
| 402 | |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 403 | void StmtDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { |
| 404 | DumpExpr(Node); |
| 405 | |
| 406 | fprintf(F, " "); |
| 407 | DumpType(Node->getEncodedType()); |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame^] | 410 | void StmtDumper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) { |
| 411 | DumpExpr(Node); |
| 412 | |
| 413 | fprintf(F, " "); |
| 414 | Selector &selector = Node->getSelector(); |
| 415 | if (selector.isUnarySelector()) |
| 416 | fprintf(F, "%s", selector.getIdentifierInfoForSlot(0)->getName()); |
| 417 | else { |
| 418 | for (unsigned i = 0, e = Node->getNumArgs(); i != e; ++i) |
| 419 | fprintf(F, "%s:", selector.getIdentifierInfoForSlot(i)->getName()); |
| 420 | } |
| 421 | } |
| 422 | |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 423 | //===----------------------------------------------------------------------===// |
| 424 | // Stmt method implementations |
| 425 | //===----------------------------------------------------------------------===// |
| 426 | |
| 427 | /// dump - This does a local dump of the specified AST fragment. It dumps the |
| 428 | /// specified node and a few nodes underneath it, but not the whole subtree. |
| 429 | /// This is useful in a debugger. |
Chris Lattner | e300c87 | 2007-08-30 06:17:34 +0000 | [diff] [blame] | 430 | void Stmt::dump(SourceManager &SM) const { |
Chris Lattner | 0c727a3 | 2007-08-30 00:40:08 +0000 | [diff] [blame] | 431 | StmtDumper P(&SM, stderr, 4); |
Chris Lattner | b393879 | 2007-08-30 00:53:54 +0000 | [diff] [blame] | 432 | P.DumpSubTree(const_cast<Stmt*>(this)); |
Chris Lattner | 0c727a3 | 2007-08-30 00:40:08 +0000 | [diff] [blame] | 433 | fprintf(stderr, "\n"); |
| 434 | } |
| 435 | |
| 436 | /// dump - This does a local dump of the specified AST fragment. It dumps the |
| 437 | /// specified node and a few nodes underneath it, but not the whole subtree. |
| 438 | /// This is useful in a debugger. |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 439 | void Stmt::dump() const { |
Chris Lattner | 0c727a3 | 2007-08-30 00:40:08 +0000 | [diff] [blame] | 440 | StmtDumper P(0, stderr, 4); |
Chris Lattner | b393879 | 2007-08-30 00:53:54 +0000 | [diff] [blame] | 441 | P.DumpSubTree(const_cast<Stmt*>(this)); |
Chris Lattner | 0c727a3 | 2007-08-30 00:40:08 +0000 | [diff] [blame] | 442 | fprintf(stderr, "\n"); |
| 443 | } |
| 444 | |
| 445 | /// dumpAll - This does a dump of the specified AST fragment and all subtrees. |
Chris Lattner | e300c87 | 2007-08-30 06:17:34 +0000 | [diff] [blame] | 446 | void Stmt::dumpAll(SourceManager &SM) const { |
Chris Lattner | 0c727a3 | 2007-08-30 00:40:08 +0000 | [diff] [blame] | 447 | StmtDumper P(&SM, stderr, ~0U); |
Chris Lattner | b393879 | 2007-08-30 00:53:54 +0000 | [diff] [blame] | 448 | P.DumpSubTree(const_cast<Stmt*>(this)); |
Chris Lattner | ee41ce5 | 2007-08-10 21:51:12 +0000 | [diff] [blame] | 449 | fprintf(stderr, "\n"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /// dumpAll - This does a dump of the specified AST fragment and all subtrees. |
| 453 | void Stmt::dumpAll() const { |
Chris Lattner | 0c727a3 | 2007-08-30 00:40:08 +0000 | [diff] [blame] | 454 | StmtDumper P(0, stderr, ~0U); |
Chris Lattner | b393879 | 2007-08-30 00:53:54 +0000 | [diff] [blame] | 455 | P.DumpSubTree(const_cast<Stmt*>(this)); |
Chris Lattner | ee41ce5 | 2007-08-10 21:51:12 +0000 | [diff] [blame] | 456 | fprintf(stderr, "\n"); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 457 | } |