blob: c2ffe4f2a74c9d24cedbeb586ad6f374250c0b89 [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);
Douglas Gregoree75c052009-05-21 20:55:50 +0000136
137 // C++
138 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
139 void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node);
140 void VisitCXXThisExpr(CXXThisExpr *Node);
141 void VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Douglas Gregoree75c052009-05-21 20:55:50 +0000143 // ObjC
144 void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
145 void VisitObjCMessageExpr(ObjCMessageExpr* Node);
146 void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
147 void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
148 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node);
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000149 void VisitObjCImplicitSetterGetterRefExpr(
150 ObjCImplicitSetterGetterRefExpr *Node);
Douglas Gregoree75c052009-05-21 20:55:50 +0000151 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
Douglas Gregor038f75a2009-06-15 19:02:54 +0000152#endif
Douglas Gregoree75c052009-05-21 20:55:50 +0000153 };
154}
155
156//===----------------------------------------------------------------------===//
157// Stmt printing methods.
158//===----------------------------------------------------------------------===//
Douglas Gregor038f75a2009-06-15 19:02:54 +0000159#if (0)
Mike Stump1eb44332009-09-09 15:08:12 +0000160void StmtXML::VisitStmt(Stmt *Node) {
Douglas Gregoree75c052009-05-21 20:55:50 +0000161 // nothing special to do
162}
163
Mike Stump1eb44332009-09-09 15:08:12 +0000164void StmtXML::VisitDeclStmt(DeclStmt *Node) {
Douglas Gregoree75c052009-05-21 20:55:50 +0000165 for (DeclStmt::decl_iterator DI = Node->decl_begin(), DE = Node->decl_end();
Mike Stump1eb44332009-09-09 15:08:12 +0000166 DI != DE; ++DI) {
Douglas Gregoree75c052009-05-21 20:55:50 +0000167 Doc.PrintDecl(*DI);
168 }
169}
170
Mike Stump1eb44332009-09-09 15:08:12 +0000171void StmtXML::VisitLabelStmt(LabelStmt *Node) {
Douglas Gregoree75c052009-05-21 20:55:50 +0000172 Doc.addAttribute("name", Node->getName());
173}
174
Mike Stump1eb44332009-09-09 15:08:12 +0000175void StmtXML::VisitGotoStmt(GotoStmt *Node) {
Douglas Gregoree75c052009-05-21 20:55:50 +0000176 Doc.addAttribute("name", Node->getLabel()->getName());
177}
178
179//===----------------------------------------------------------------------===//
180// Expr printing methods.
181//===----------------------------------------------------------------------===//
182
183void StmtXML::VisitExpr(Expr *Node) {
184 DumpExpr(Node);
185}
186
187void StmtXML::VisitDeclRefExpr(DeclRefExpr *Node) {
188 DumpExpr(Node);
189
190 const char* pKind;
191 switch (Node->getDecl()->getKind()) {
Mike Stumpb7166332010-01-20 02:03:14 +0000192 case Decl::Function: pKind = "FunctionDecl"; break;
193 case Decl::Var: pKind = "Var"; break;
194 case Decl::ParmVar: pKind = "ParmVar"; break;
195 case Decl::EnumConstant: pKind = "EnumConstant"; break;
196 case Decl::Typedef: pKind = "Typedef"; break;
197 case Decl::Record: pKind = "Record"; break;
198 case Decl::Enum: pKind = "Enum"; break;
199 case Decl::CXXRecord: pKind = "CXXRecord"; break;
200 case Decl::ObjCInterface: pKind = "ObjCInterface"; break;
201 case Decl::ObjCClass: pKind = "ObjCClass"; break;
202 default: pKind = "Decl"; break;
Douglas Gregoree75c052009-05-21 20:55:50 +0000203 }
204
205 Doc.addAttribute("kind", pKind);
206 Doc.addAttribute("name", Node->getDecl()->getNameAsString());
207 Doc.addRefAttribute(Node->getDecl());
208}
209
210void StmtXML::VisitPredefinedExpr(PredefinedExpr *Node) {
211 DumpExpr(Node);
212 switch (Node->getIdentType()) {
Mike Stumpb7166332010-01-20 02:03:14 +0000213 default: assert(0 && "unknown case");
214 case PredefinedExpr::Func: Doc.addAttribute("predefined", " __func__"); break;
215 case PredefinedExpr::Function: Doc.addAttribute("predefined", " __FUNCTION__"); break;
216 case PredefinedExpr::PrettyFunction: Doc.addAttribute("predefined", " __PRETTY_FUNCTION__");break;
Douglas Gregoree75c052009-05-21 20:55:50 +0000217 }
218}
219
220void StmtXML::VisitCharacterLiteral(CharacterLiteral *Node) {
221 DumpExpr(Node);
222 Doc.addAttribute("value", Node->getValue());
223}
224
225void StmtXML::VisitIntegerLiteral(IntegerLiteral *Node) {
226 DumpExpr(Node);
227 bool isSigned = Node->getType()->isSignedIntegerType();
228 Doc.addAttribute("value", Node->getValue().toString(10, isSigned));
229}
230
231void StmtXML::VisitFloatingLiteral(FloatingLiteral *Node) {
232 DumpExpr(Node);
233 // FIXME: output float as written in source (no approximation or the like)
234 //Doc.addAttribute("value", Node->getValueAsApproximateDouble()));
235 Doc.addAttribute("value", "FIXME");
236}
237
238void StmtXML::VisitStringLiteral(StringLiteral *Str) {
239 DumpExpr(Str);
240 if (Str->isWide())
241 Doc.addAttribute("is_wide", "1");
242
243 Doc.addAttribute("value", Doc.escapeString(Str->getStrData(), Str->getByteLength()));
244}
245
246
247const char *StmtXML::getOpcodeStr(UnaryOperator::Opcode Op) {
248 switch (Op) {
249 default: assert(0 && "Unknown unary operator");
250 case UnaryOperator::PostInc: return "postinc";
251 case UnaryOperator::PostDec: return "postdec";
252 case UnaryOperator::PreInc: return "preinc";
253 case UnaryOperator::PreDec: return "predec";
254 case UnaryOperator::AddrOf: return "addrof";
255 case UnaryOperator::Deref: return "deref";
256 case UnaryOperator::Plus: return "plus";
257 case UnaryOperator::Minus: return "minus";
258 case UnaryOperator::Not: return "not";
259 case UnaryOperator::LNot: return "lnot";
260 case UnaryOperator::Real: return "__real";
261 case UnaryOperator::Imag: return "__imag";
262 case UnaryOperator::Extension: return "__extension__";
Douglas Gregoree75c052009-05-21 20:55:50 +0000263 }
264}
265
266
267const char *StmtXML::getOpcodeStr(BinaryOperator::Opcode Op) {
268 switch (Op) {
269 default: assert(0 && "Unknown binary operator");
270 case BinaryOperator::PtrMemD: return "ptrmemd";
271 case BinaryOperator::PtrMemI: return "ptrmemi";
272 case BinaryOperator::Mul: return "mul";
273 case BinaryOperator::Div: return "div";
274 case BinaryOperator::Rem: return "rem";
275 case BinaryOperator::Add: return "add";
276 case BinaryOperator::Sub: return "sub";
277 case BinaryOperator::Shl: return "shl";
278 case BinaryOperator::Shr: return "shr";
279 case BinaryOperator::LT: return "lt";
280 case BinaryOperator::GT: return "gt";
281 case BinaryOperator::LE: return "le";
282 case BinaryOperator::GE: return "ge";
283 case BinaryOperator::EQ: return "eq";
284 case BinaryOperator::NE: return "ne";
285 case BinaryOperator::And: return "and";
286 case BinaryOperator::Xor: return "xor";
287 case BinaryOperator::Or: return "or";
288 case BinaryOperator::LAnd: return "land";
289 case BinaryOperator::LOr: return "lor";
290 case BinaryOperator::Assign: return "assign";
291 case BinaryOperator::MulAssign: return "mulassign";
292 case BinaryOperator::DivAssign: return "divassign";
293 case BinaryOperator::RemAssign: return "remassign";
294 case BinaryOperator::AddAssign: return "addassign";
295 case BinaryOperator::SubAssign: return "subassign";
296 case BinaryOperator::ShlAssign: return "shlassign";
297 case BinaryOperator::ShrAssign: return "shrassign";
298 case BinaryOperator::AndAssign: return "andassign";
299 case BinaryOperator::XorAssign: return "xorassign";
300 case BinaryOperator::OrAssign: return "orassign";
301 case BinaryOperator::Comma: return "comma";
302 }
303}
304
305void StmtXML::VisitUnaryOperator(UnaryOperator *Node) {
306 DumpExpr(Node);
307 Doc.addAttribute("op_code", getOpcodeStr(Node->getOpcode()));
308}
309
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000310void StmtXML::OffsetOfExpr(OffsetOfExpr *Node) {
311 DumpExpr(Node);
312}
313
Douglas Gregoree75c052009-05-21 20:55:50 +0000314void StmtXML::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
315 DumpExpr(Node);
316 Doc.addAttribute("is_sizeof", Node->isSizeOf() ? "sizeof" : "alignof");
317 Doc.addAttribute("is_type", Node->isArgumentType() ? "1" : "0");
318 if (Node->isArgumentType())
Douglas Gregoree75c052009-05-21 20:55:50 +0000319 DumpTypeExpr(Node->getArgumentType());
Douglas Gregoree75c052009-05-21 20:55:50 +0000320}
321
322void StmtXML::VisitMemberExpr(MemberExpr *Node) {
323 DumpExpr(Node);
324 Doc.addAttribute("is_deref", Node->isArrow() ? "1" : "0");
325 Doc.addAttribute("name", Node->getMemberDecl()->getNameAsString());
326 Doc.addRefAttribute(Node->getMemberDecl());
327}
328
329void StmtXML::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
330 DumpExpr(Node);
331 Doc.addAttribute("name", Node->getAccessor().getName());
332}
333
334void StmtXML::VisitBinaryOperator(BinaryOperator *Node) {
335 DumpExpr(Node);
336 Doc.addAttribute("op_code", getOpcodeStr(Node->getOpcode()));
337}
338
339void StmtXML::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
340 VisitBinaryOperator(Node);
341/* FIXME: is this needed in the AST?
342 DumpExpr(Node);
343 CurrentNode = CurrentNode->addSubNode("ComputeLHSTy");
344 DumpType(Node->getComputationLHSType());
345 CurrentNode = CurrentNode->Parent->addSubNode("ComputeResultTy");
346 DumpType(Node->getComputationResultType());
347 Doc.toParent();
348*/
349}
350
351// GNU extensions.
352
353void StmtXML::VisitAddrLabelExpr(AddrLabelExpr *Node) {
354 DumpExpr(Node);
355 Doc.addAttribute("name", Node->getLabel()->getName());
356}
357
Douglas Gregoree75c052009-05-21 20:55:50 +0000358//===----------------------------------------------------------------------===//
359// C++ Expressions
360//===----------------------------------------------------------------------===//
361
362void StmtXML::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
363 DumpExpr(Node);
364 Doc.addAttribute("kind", Node->getCastName());
365 DumpTypeExpr(Node->getTypeAsWritten());
366}
367
368void StmtXML::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
369 DumpExpr(Node);
370 Doc.addAttribute("value", Node->getValue() ? "true" : "false");
371}
372
373void StmtXML::VisitCXXThisExpr(CXXThisExpr *Node) {
374 DumpExpr(Node);
375}
376
377void StmtXML::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
378 DumpExpr(Node);
379 DumpTypeExpr(Node->getTypeAsWritten());
380}
381
382//===----------------------------------------------------------------------===//
383// Obj-C Expressions
384//===----------------------------------------------------------------------===//
385
386void StmtXML::VisitObjCMessageExpr(ObjCMessageExpr* Node) {
387 DumpExpr(Node);
388 Doc.addAttribute("selector", Node->getSelector().getAsString());
389 IdentifierInfo* clsName = Node->getClassName();
Mike Stump1eb44332009-09-09 15:08:12 +0000390 if (clsName)
Douglas Gregoree75c052009-05-21 20:55:50 +0000391 Doc.addAttribute("class", clsName->getName());
392}
393
394void StmtXML::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
395 DumpExpr(Node);
396 DumpTypeExpr(Node->getEncodedType());
397}
398
399void StmtXML::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
400 DumpExpr(Node);
401 Doc.addAttribute("selector", Node->getSelector().getAsString());
402}
403
404void StmtXML::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
405 DumpExpr(Node);
406 Doc.addAttribute("protocol", Node->getProtocol()->getNameAsString());
407}
408
409void StmtXML::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
410 DumpExpr(Node);
411 Doc.addAttribute("property", Node->getProperty()->getNameAsString());
412}
413
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000414void StmtXML::VisitObjCImplicitSetterGetterRefExpr(
415 ObjCImplicitSetterGetterRefExpr *Node) {
Douglas Gregoree75c052009-05-21 20:55:50 +0000416 DumpExpr(Node);
417 ObjCMethodDecl *Getter = Node->getGetterMethod();
418 ObjCMethodDecl *Setter = Node->getSetterMethod();
419 Doc.addAttribute("Getter", Getter->getSelector().getAsString());
420 Doc.addAttribute("Setter", Setter ? Setter->getSelector().getAsString().c_str() : "(null)");
421}
422
Douglas Gregoree75c052009-05-21 20:55:50 +0000423void StmtXML::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
424 DumpExpr(Node);
425 Doc.addAttribute("kind", Node->getDecl()->getDeclKindName());
426 Doc.addAttribute("decl", Node->getDecl()->getNameAsString());
427 if (Node->isFreeIvar())
428 Doc.addAttribute("isFreeIvar", "1");
429}
Douglas Gregor038f75a2009-06-15 19:02:54 +0000430#endif
Douglas Gregoree75c052009-05-21 20:55:50 +0000431//===----------------------------------------------------------------------===//
432// Stmt method implementations
433//===----------------------------------------------------------------------===//
434
435/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
436void DocumentXML::PrintStmt(const Stmt *S) {
437 StmtXML P(*this);
438 P.DumpSubTree(const_cast<Stmt*>(S));
439}
440