blob: fdd40b15c94ca6e57a000cfb442767610b2bc684 [file] [log] [blame]
Douglas Gregoree75c052009-05-21 20:55:50 +00001//===--- StmtXML.cpp - XML implementation for Stmt ASTs ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Stmt::dumpXML methods, which dump out the
11// AST to an XML document.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Frontend/DocumentXML.h"
16#include "clang/AST/StmtVisitor.h"
17#include "clang/AST/DeclObjC.h"
18#include "clang/AST/DeclCXX.h"
19#include "clang/Basic/SourceManager.h"
Douglas Gregoree75c052009-05-21 20:55:50 +000020using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// StmtXML Visitor
24//===----------------------------------------------------------------------===//
25
26namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +000027 class StmtXML : public StmtVisitor<StmtXML> {
Douglas Gregoree75c052009-05-21 20:55:50 +000028 DocumentXML& Doc;
29
Douglas Gregor038f75a2009-06-15 19:02:54 +000030 //static const char *getOpcodeStr(UnaryOperator::Opcode Op);
31 //static const char *getOpcodeStr(BinaryOperator::Opcode Op);
32
33
Mike Stump1eb44332009-09-09 15:08:12 +000034 void addSpecialAttribute(const char* pName, StringLiteral* Str) {
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +000035 Doc.addAttribute(pName, Doc.escapeString(Str->getString().data(),
36 Str->getString().size()));
Douglas Gregor038f75a2009-06-15 19:02:54 +000037 }
38
Mike Stump1eb44332009-09-09 15:08:12 +000039 void addSpecialAttribute(const char* pName, SizeOfAlignOfExpr* S) {
Douglas Gregor038f75a2009-06-15 19:02:54 +000040 if (S->isArgumentType())
Douglas Gregor038f75a2009-06-15 19:02:54 +000041 Doc.addAttribute(pName, S->getArgumentType());
Douglas Gregor038f75a2009-06-15 19:02:54 +000042 }
43
Mike Stump1eb44332009-09-09 15:08:12 +000044 void addSpecialAttribute(const char* pName, CXXTypeidExpr* S) {
Douglas Gregor038f75a2009-06-15 19:02:54 +000045 if (S->isTypeOperand())
Douglas Gregor038f75a2009-06-15 19:02:54 +000046 Doc.addAttribute(pName, S->getTypeOperand());
Douglas Gregor038f75a2009-06-15 19:02:54 +000047 }
48
Douglas Gregoree75c052009-05-21 20:55:50 +000049
50 public:
51 StmtXML(DocumentXML& doc)
52 : Doc(doc) {
53 }
Mike Stump1eb44332009-09-09 15:08:12 +000054
Douglas Gregoree75c052009-05-21 20:55:50 +000055 void DumpSubTree(Stmt *S) {
Mike Stump1eb44332009-09-09 15:08:12 +000056 if (S) {
Douglas Gregor038f75a2009-06-15 19:02:54 +000057 Visit(S);
Mike Stump1eb44332009-09-09 15:08:12 +000058 if (DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
59 for (DeclStmt::decl_iterator DI = DS->decl_begin(),
60 DE = DS->decl_end(); DI != DE; ++DI) {
Douglas Gregor038f75a2009-06-15 19:02:54 +000061 Doc.PrintDecl(*DI);
62 }
Mike Stump1eb44332009-09-09 15:08:12 +000063 } else {
Mike Stump1eb44332009-09-09 15:08:12 +000064 for (Stmt::child_iterator i = S->child_begin(), e = S->child_end();
65 i != e; ++i)
Douglas Gregoree75c052009-05-21 20:55:50 +000066 DumpSubTree(*i);
Douglas Gregoree75c052009-05-21 20:55:50 +000067 }
68 Doc.toParent();
69 } else {
70 Doc.addSubNode("NULL").toParent();
71 }
72 }
73
Douglas Gregoree75c052009-05-21 20:55:50 +000074
Douglas Gregor038f75a2009-06-15 19:02:54 +000075#define NODE_XML( CLASS, NAME ) \
76 void Visit##CLASS(CLASS* S) \
77 { \
78 typedef CLASS tStmtType; \
Mike Stump1eb44332009-09-09 15:08:12 +000079 Doc.addSubNode(NAME);
Douglas Gregoree75c052009-05-21 20:55:50 +000080
Mike Stump1eb44332009-09-09 15:08:12 +000081#define ATTRIBUTE_XML( FN, NAME ) Doc.addAttribute(NAME, S->FN);
Douglas Gregor038f75a2009-06-15 19:02:54 +000082#define TYPE_ATTRIBUTE_XML( FN ) ATTRIBUTE_XML(FN, "type")
Mike Stump1eb44332009-09-09 15:08:12 +000083#define ATTRIBUTE_OPT_XML( FN, NAME ) Doc.addAttributeOptional(NAME, S->FN);
84#define ATTRIBUTE_SPECIAL_XML( FN, NAME ) addSpecialAttribute(NAME, S);
Douglas Gregor038f75a2009-06-15 19:02:54 +000085#define ATTRIBUTE_FILE_LOCATION_XML Doc.addLocationRange(S->getSourceRange());
86
87
88#define ATTRIBUTE_ENUM_XML( FN, NAME ) \
89 { \
90 const char* pAttributeName = NAME; \
91 const bool optional = false; \
92 switch (S->FN) { \
Mike Stumpb7166332010-01-20 02:03:14 +000093 default: assert(0 && "unknown enum value");
Douglas Gregor038f75a2009-06-15 19:02:54 +000094
95#define ATTRIBUTE_ENUM_OPT_XML( FN, NAME ) \
96 { \
97 const char* pAttributeName = NAME; \
98 const bool optional = true; \
99 switch (S->FN) { \
Mike Stumpb7166332010-01-20 02:03:14 +0000100 default: assert(0 && "unknown enum value");
Douglas Gregor038f75a2009-06-15 19:02:54 +0000101
102#define ENUM_XML( VALUE, NAME ) case VALUE: if ((!optional) || NAME[0]) Doc.addAttribute(pAttributeName, NAME); break;
103#define END_ENUM_XML } }
104#define END_NODE_XML }
105
106#define ID_ATTRIBUTE_XML Doc.addAttribute("id", S);
107#define SUB_NODE_XML( CLASS )
108#define SUB_NODE_SEQUENCE_XML( CLASS )
109#define SUB_NODE_OPT_XML( CLASS )
110
111#include "clang/Frontend/StmtXML.def"
112
113#if (0)
Douglas Gregoree75c052009-05-21 20:55:50 +0000114 // Stmts.
115 void VisitStmt(Stmt *Node);
116 void VisitDeclStmt(DeclStmt *Node);
117 void VisitLabelStmt(LabelStmt *Node);
118 void VisitGotoStmt(GotoStmt *Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Douglas Gregoree75c052009-05-21 20:55:50 +0000120 // Exprs
121 void VisitExpr(Expr *Node);
122 void VisitDeclRefExpr(DeclRefExpr *Node);
123 void VisitPredefinedExpr(PredefinedExpr *Node);
124 void VisitCharacterLiteral(CharacterLiteral *Node);
125 void VisitIntegerLiteral(IntegerLiteral *Node);
126 void VisitFloatingLiteral(FloatingLiteral *Node);
127 void VisitStringLiteral(StringLiteral *Str);
128 void VisitUnaryOperator(UnaryOperator *Node);
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000129 void VisitOffsetOfExpr(OffsetOfExpr *Node);
Douglas Gregoree75c052009-05-21 20:55:50 +0000130 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node);
131 void VisitMemberExpr(MemberExpr *Node);
132 void VisitExtVectorElementExpr(ExtVectorElementExpr *Node);
133 void VisitBinaryOperator(BinaryOperator *Node);
134 void VisitCompoundAssignOperator(CompoundAssignOperator *Node);
135 void VisitAddrLabelExpr(AddrLabelExpr *Node);
136 void VisitTypesCompatibleExpr(TypesCompatibleExpr *Node);
137
138 // C++
139 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
140 void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node);
141 void VisitCXXThisExpr(CXXThisExpr *Node);
142 void VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Douglas Gregoree75c052009-05-21 20:55:50 +0000144 // ObjC
145 void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
146 void VisitObjCMessageExpr(ObjCMessageExpr* Node);
147 void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
148 void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
149 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node);
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000150 void VisitObjCImplicitSetterGetterRefExpr(
151 ObjCImplicitSetterGetterRefExpr *Node);
Douglas Gregoree75c052009-05-21 20:55:50 +0000152 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
Douglas Gregor038f75a2009-06-15 19:02:54 +0000153#endif
Douglas Gregoree75c052009-05-21 20:55:50 +0000154 };
155}
156
157//===----------------------------------------------------------------------===//
158// Stmt printing methods.
159//===----------------------------------------------------------------------===//
Douglas Gregor038f75a2009-06-15 19:02:54 +0000160#if (0)
Mike Stump1eb44332009-09-09 15:08:12 +0000161void StmtXML::VisitStmt(Stmt *Node) {
Douglas Gregoree75c052009-05-21 20:55:50 +0000162 // nothing special to do
163}
164
Mike Stump1eb44332009-09-09 15:08:12 +0000165void StmtXML::VisitDeclStmt(DeclStmt *Node) {
Douglas Gregoree75c052009-05-21 20:55:50 +0000166 for (DeclStmt::decl_iterator DI = Node->decl_begin(), DE = Node->decl_end();
Mike Stump1eb44332009-09-09 15:08:12 +0000167 DI != DE; ++DI) {
Douglas Gregoree75c052009-05-21 20:55:50 +0000168 Doc.PrintDecl(*DI);
169 }
170}
171
Mike Stump1eb44332009-09-09 15:08:12 +0000172void StmtXML::VisitLabelStmt(LabelStmt *Node) {
Douglas Gregoree75c052009-05-21 20:55:50 +0000173 Doc.addAttribute("name", Node->getName());
174}
175
Mike Stump1eb44332009-09-09 15:08:12 +0000176void StmtXML::VisitGotoStmt(GotoStmt *Node) {
Douglas Gregoree75c052009-05-21 20:55:50 +0000177 Doc.addAttribute("name", Node->getLabel()->getName());
178}
179
180//===----------------------------------------------------------------------===//
181// Expr printing methods.
182//===----------------------------------------------------------------------===//
183
184void StmtXML::VisitExpr(Expr *Node) {
185 DumpExpr(Node);
186}
187
188void StmtXML::VisitDeclRefExpr(DeclRefExpr *Node) {
189 DumpExpr(Node);
190
191 const char* pKind;
192 switch (Node->getDecl()->getKind()) {
Mike Stumpb7166332010-01-20 02:03:14 +0000193 case Decl::Function: pKind = "FunctionDecl"; break;
194 case Decl::Var: pKind = "Var"; break;
195 case Decl::ParmVar: pKind = "ParmVar"; break;
196 case Decl::EnumConstant: pKind = "EnumConstant"; break;
197 case Decl::Typedef: pKind = "Typedef"; break;
198 case Decl::Record: pKind = "Record"; break;
199 case Decl::Enum: pKind = "Enum"; break;
200 case Decl::CXXRecord: pKind = "CXXRecord"; break;
201 case Decl::ObjCInterface: pKind = "ObjCInterface"; break;
202 case Decl::ObjCClass: pKind = "ObjCClass"; break;
203 default: pKind = "Decl"; break;
Douglas Gregoree75c052009-05-21 20:55:50 +0000204 }
205
206 Doc.addAttribute("kind", pKind);
207 Doc.addAttribute("name", Node->getDecl()->getNameAsString());
208 Doc.addRefAttribute(Node->getDecl());
209}
210
211void StmtXML::VisitPredefinedExpr(PredefinedExpr *Node) {
212 DumpExpr(Node);
213 switch (Node->getIdentType()) {
Mike Stumpb7166332010-01-20 02:03:14 +0000214 default: assert(0 && "unknown case");
215 case PredefinedExpr::Func: Doc.addAttribute("predefined", " __func__"); break;
216 case PredefinedExpr::Function: Doc.addAttribute("predefined", " __FUNCTION__"); break;
217 case PredefinedExpr::PrettyFunction: Doc.addAttribute("predefined", " __PRETTY_FUNCTION__");break;
Douglas Gregoree75c052009-05-21 20:55:50 +0000218 }
219}
220
221void StmtXML::VisitCharacterLiteral(CharacterLiteral *Node) {
222 DumpExpr(Node);
223 Doc.addAttribute("value", Node->getValue());
224}
225
226void StmtXML::VisitIntegerLiteral(IntegerLiteral *Node) {
227 DumpExpr(Node);
228 bool isSigned = Node->getType()->isSignedIntegerType();
229 Doc.addAttribute("value", Node->getValue().toString(10, isSigned));
230}
231
232void StmtXML::VisitFloatingLiteral(FloatingLiteral *Node) {
233 DumpExpr(Node);
234 // FIXME: output float as written in source (no approximation or the like)
235 //Doc.addAttribute("value", Node->getValueAsApproximateDouble()));
236 Doc.addAttribute("value", "FIXME");
237}
238
239void StmtXML::VisitStringLiteral(StringLiteral *Str) {
240 DumpExpr(Str);
241 if (Str->isWide())
242 Doc.addAttribute("is_wide", "1");
243
244 Doc.addAttribute("value", Doc.escapeString(Str->getStrData(), Str->getByteLength()));
245}
246
247
248const char *StmtXML::getOpcodeStr(UnaryOperator::Opcode Op) {
249 switch (Op) {
250 default: assert(0 && "Unknown unary operator");
251 case UnaryOperator::PostInc: return "postinc";
252 case UnaryOperator::PostDec: return "postdec";
253 case UnaryOperator::PreInc: return "preinc";
254 case UnaryOperator::PreDec: return "predec";
255 case UnaryOperator::AddrOf: return "addrof";
256 case UnaryOperator::Deref: return "deref";
257 case UnaryOperator::Plus: return "plus";
258 case UnaryOperator::Minus: return "minus";
259 case UnaryOperator::Not: return "not";
260 case UnaryOperator::LNot: return "lnot";
261 case UnaryOperator::Real: return "__real";
262 case UnaryOperator::Imag: return "__imag";
263 case UnaryOperator::Extension: return "__extension__";
Douglas Gregoree75c052009-05-21 20:55:50 +0000264 }
265}
266
267
268const char *StmtXML::getOpcodeStr(BinaryOperator::Opcode Op) {
269 switch (Op) {
270 default: assert(0 && "Unknown binary operator");
271 case BinaryOperator::PtrMemD: return "ptrmemd";
272 case BinaryOperator::PtrMemI: return "ptrmemi";
273 case BinaryOperator::Mul: return "mul";
274 case BinaryOperator::Div: return "div";
275 case BinaryOperator::Rem: return "rem";
276 case BinaryOperator::Add: return "add";
277 case BinaryOperator::Sub: return "sub";
278 case BinaryOperator::Shl: return "shl";
279 case BinaryOperator::Shr: return "shr";
280 case BinaryOperator::LT: return "lt";
281 case BinaryOperator::GT: return "gt";
282 case BinaryOperator::LE: return "le";
283 case BinaryOperator::GE: return "ge";
284 case BinaryOperator::EQ: return "eq";
285 case BinaryOperator::NE: return "ne";
286 case BinaryOperator::And: return "and";
287 case BinaryOperator::Xor: return "xor";
288 case BinaryOperator::Or: return "or";
289 case BinaryOperator::LAnd: return "land";
290 case BinaryOperator::LOr: return "lor";
291 case BinaryOperator::Assign: return "assign";
292 case BinaryOperator::MulAssign: return "mulassign";
293 case BinaryOperator::DivAssign: return "divassign";
294 case BinaryOperator::RemAssign: return "remassign";
295 case BinaryOperator::AddAssign: return "addassign";
296 case BinaryOperator::SubAssign: return "subassign";
297 case BinaryOperator::ShlAssign: return "shlassign";
298 case BinaryOperator::ShrAssign: return "shrassign";
299 case BinaryOperator::AndAssign: return "andassign";
300 case BinaryOperator::XorAssign: return "xorassign";
301 case BinaryOperator::OrAssign: return "orassign";
302 case BinaryOperator::Comma: return "comma";
303 }
304}
305
306void StmtXML::VisitUnaryOperator(UnaryOperator *Node) {
307 DumpExpr(Node);
308 Doc.addAttribute("op_code", getOpcodeStr(Node->getOpcode()));
309}
310
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000311void StmtXML::OffsetOfExpr(OffsetOfExpr *Node) {
312 DumpExpr(Node);
313}
314
Douglas Gregoree75c052009-05-21 20:55:50 +0000315void StmtXML::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
316 DumpExpr(Node);
317 Doc.addAttribute("is_sizeof", Node->isSizeOf() ? "sizeof" : "alignof");
318 Doc.addAttribute("is_type", Node->isArgumentType() ? "1" : "0");
319 if (Node->isArgumentType())
Douglas Gregoree75c052009-05-21 20:55:50 +0000320 DumpTypeExpr(Node->getArgumentType());
Douglas Gregoree75c052009-05-21 20:55:50 +0000321}
322
323void StmtXML::VisitMemberExpr(MemberExpr *Node) {
324 DumpExpr(Node);
325 Doc.addAttribute("is_deref", Node->isArrow() ? "1" : "0");
326 Doc.addAttribute("name", Node->getMemberDecl()->getNameAsString());
327 Doc.addRefAttribute(Node->getMemberDecl());
328}
329
330void StmtXML::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
331 DumpExpr(Node);
332 Doc.addAttribute("name", Node->getAccessor().getName());
333}
334
335void StmtXML::VisitBinaryOperator(BinaryOperator *Node) {
336 DumpExpr(Node);
337 Doc.addAttribute("op_code", getOpcodeStr(Node->getOpcode()));
338}
339
340void StmtXML::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
341 VisitBinaryOperator(Node);
342/* FIXME: is this needed in the AST?
343 DumpExpr(Node);
344 CurrentNode = CurrentNode->addSubNode("ComputeLHSTy");
345 DumpType(Node->getComputationLHSType());
346 CurrentNode = CurrentNode->Parent->addSubNode("ComputeResultTy");
347 DumpType(Node->getComputationResultType());
348 Doc.toParent();
349*/
350}
351
352// GNU extensions.
353
354void StmtXML::VisitAddrLabelExpr(AddrLabelExpr *Node) {
355 DumpExpr(Node);
356 Doc.addAttribute("name", Node->getLabel()->getName());
357}
358
359void StmtXML::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
360 DumpExpr(Node);
361 DumpTypeExpr(Node->getArgType1());
362 DumpTypeExpr(Node->getArgType2());
363}
364
365//===----------------------------------------------------------------------===//
366// C++ Expressions
367//===----------------------------------------------------------------------===//
368
369void StmtXML::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
370 DumpExpr(Node);
371 Doc.addAttribute("kind", Node->getCastName());
372 DumpTypeExpr(Node->getTypeAsWritten());
373}
374
375void StmtXML::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
376 DumpExpr(Node);
377 Doc.addAttribute("value", Node->getValue() ? "true" : "false");
378}
379
380void StmtXML::VisitCXXThisExpr(CXXThisExpr *Node) {
381 DumpExpr(Node);
382}
383
384void StmtXML::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
385 DumpExpr(Node);
386 DumpTypeExpr(Node->getTypeAsWritten());
387}
388
389//===----------------------------------------------------------------------===//
390// Obj-C Expressions
391//===----------------------------------------------------------------------===//
392
393void StmtXML::VisitObjCMessageExpr(ObjCMessageExpr* Node) {
394 DumpExpr(Node);
395 Doc.addAttribute("selector", Node->getSelector().getAsString());
396 IdentifierInfo* clsName = Node->getClassName();
Mike Stump1eb44332009-09-09 15:08:12 +0000397 if (clsName)
Douglas Gregoree75c052009-05-21 20:55:50 +0000398 Doc.addAttribute("class", clsName->getName());
399}
400
401void StmtXML::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
402 DumpExpr(Node);
403 DumpTypeExpr(Node->getEncodedType());
404}
405
406void StmtXML::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
407 DumpExpr(Node);
408 Doc.addAttribute("selector", Node->getSelector().getAsString());
409}
410
411void StmtXML::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
412 DumpExpr(Node);
413 Doc.addAttribute("protocol", Node->getProtocol()->getNameAsString());
414}
415
416void StmtXML::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
417 DumpExpr(Node);
418 Doc.addAttribute("property", Node->getProperty()->getNameAsString());
419}
420
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000421void StmtXML::VisitObjCImplicitSetterGetterRefExpr(
422 ObjCImplicitSetterGetterRefExpr *Node) {
Douglas Gregoree75c052009-05-21 20:55:50 +0000423 DumpExpr(Node);
424 ObjCMethodDecl *Getter = Node->getGetterMethod();
425 ObjCMethodDecl *Setter = Node->getSetterMethod();
426 Doc.addAttribute("Getter", Getter->getSelector().getAsString());
427 Doc.addAttribute("Setter", Setter ? Setter->getSelector().getAsString().c_str() : "(null)");
428}
429
Douglas Gregoree75c052009-05-21 20:55:50 +0000430void StmtXML::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
431 DumpExpr(Node);
432 Doc.addAttribute("kind", Node->getDecl()->getDeclKindName());
433 Doc.addAttribute("decl", Node->getDecl()->getNameAsString());
434 if (Node->isFreeIvar())
435 Doc.addAttribute("isFreeIvar", "1");
436}
Douglas Gregor038f75a2009-06-15 19:02:54 +0000437#endif
Douglas Gregoree75c052009-05-21 20:55:50 +0000438//===----------------------------------------------------------------------===//
439// Stmt method implementations
440//===----------------------------------------------------------------------===//
441
442/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
443void DocumentXML::PrintStmt(const Stmt *S) {
444 StmtXML P(*this);
445 P.DumpSubTree(const_cast<Stmt*>(S));
446}
447