blob: b98417fc3190f302b97313ad90076deacd251487 [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"
20#include "llvm/Support/Compiler.h"
21using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// StmtXML Visitor
25//===----------------------------------------------------------------------===//
26
27namespace {
28 class VISIBILITY_HIDDEN StmtXML : public StmtVisitor<StmtXML> {
29 DocumentXML& Doc;
30
Douglas Gregor038f75a2009-06-15 19:02:54 +000031 //static const char *getOpcodeStr(UnaryOperator::Opcode Op);
32 //static const char *getOpcodeStr(BinaryOperator::Opcode Op);
33
34
Mike Stump1eb44332009-09-09 15:08:12 +000035 void addSpecialAttribute(const char* pName, StringLiteral* Str) {
Douglas Gregor038f75a2009-06-15 19:02:54 +000036 Doc.addAttribute(pName, Doc.escapeString(Str->getStrData(), Str->getByteLength()));
37 }
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 Stump1eb44332009-09-09 15:08:12 +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 Stump1eb44332009-09-09 15:08:12 +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);
129 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node);
130 void VisitMemberExpr(MemberExpr *Node);
131 void VisitExtVectorElementExpr(ExtVectorElementExpr *Node);
132 void VisitBinaryOperator(BinaryOperator *Node);
133 void VisitCompoundAssignOperator(CompoundAssignOperator *Node);
134 void VisitAddrLabelExpr(AddrLabelExpr *Node);
135 void VisitTypesCompatibleExpr(TypesCompatibleExpr *Node);
136
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);
152 void VisitObjCSuperExpr(ObjCSuperExpr *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()) {
193 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;
204 }
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()) {
214 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;
218 }
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__";
264 case UnaryOperator::OffsetOf: return "__builtin_offsetof";
265 }
266}
267
268
269const char *StmtXML::getOpcodeStr(BinaryOperator::Opcode Op) {
270 switch (Op) {
271 default: assert(0 && "Unknown binary operator");
272 case BinaryOperator::PtrMemD: return "ptrmemd";
273 case BinaryOperator::PtrMemI: return "ptrmemi";
274 case BinaryOperator::Mul: return "mul";
275 case BinaryOperator::Div: return "div";
276 case BinaryOperator::Rem: return "rem";
277 case BinaryOperator::Add: return "add";
278 case BinaryOperator::Sub: return "sub";
279 case BinaryOperator::Shl: return "shl";
280 case BinaryOperator::Shr: return "shr";
281 case BinaryOperator::LT: return "lt";
282 case BinaryOperator::GT: return "gt";
283 case BinaryOperator::LE: return "le";
284 case BinaryOperator::GE: return "ge";
285 case BinaryOperator::EQ: return "eq";
286 case BinaryOperator::NE: return "ne";
287 case BinaryOperator::And: return "and";
288 case BinaryOperator::Xor: return "xor";
289 case BinaryOperator::Or: return "or";
290 case BinaryOperator::LAnd: return "land";
291 case BinaryOperator::LOr: return "lor";
292 case BinaryOperator::Assign: return "assign";
293 case BinaryOperator::MulAssign: return "mulassign";
294 case BinaryOperator::DivAssign: return "divassign";
295 case BinaryOperator::RemAssign: return "remassign";
296 case BinaryOperator::AddAssign: return "addassign";
297 case BinaryOperator::SubAssign: return "subassign";
298 case BinaryOperator::ShlAssign: return "shlassign";
299 case BinaryOperator::ShrAssign: return "shrassign";
300 case BinaryOperator::AndAssign: return "andassign";
301 case BinaryOperator::XorAssign: return "xorassign";
302 case BinaryOperator::OrAssign: return "orassign";
303 case BinaryOperator::Comma: return "comma";
304 }
305}
306
307void StmtXML::VisitUnaryOperator(UnaryOperator *Node) {
308 DumpExpr(Node);
309 Doc.addAttribute("op_code", getOpcodeStr(Node->getOpcode()));
310}
311
312void StmtXML::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
313 DumpExpr(Node);
314 Doc.addAttribute("is_sizeof", Node->isSizeOf() ? "sizeof" : "alignof");
315 Doc.addAttribute("is_type", Node->isArgumentType() ? "1" : "0");
316 if (Node->isArgumentType())
Douglas Gregoree75c052009-05-21 20:55:50 +0000317 DumpTypeExpr(Node->getArgumentType());
Douglas Gregoree75c052009-05-21 20:55:50 +0000318}
319
320void StmtXML::VisitMemberExpr(MemberExpr *Node) {
321 DumpExpr(Node);
322 Doc.addAttribute("is_deref", Node->isArrow() ? "1" : "0");
323 Doc.addAttribute("name", Node->getMemberDecl()->getNameAsString());
324 Doc.addRefAttribute(Node->getMemberDecl());
325}
326
327void StmtXML::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
328 DumpExpr(Node);
329 Doc.addAttribute("name", Node->getAccessor().getName());
330}
331
332void StmtXML::VisitBinaryOperator(BinaryOperator *Node) {
333 DumpExpr(Node);
334 Doc.addAttribute("op_code", getOpcodeStr(Node->getOpcode()));
335}
336
337void StmtXML::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
338 VisitBinaryOperator(Node);
339/* FIXME: is this needed in the AST?
340 DumpExpr(Node);
341 CurrentNode = CurrentNode->addSubNode("ComputeLHSTy");
342 DumpType(Node->getComputationLHSType());
343 CurrentNode = CurrentNode->Parent->addSubNode("ComputeResultTy");
344 DumpType(Node->getComputationResultType());
345 Doc.toParent();
346*/
347}
348
349// GNU extensions.
350
351void StmtXML::VisitAddrLabelExpr(AddrLabelExpr *Node) {
352 DumpExpr(Node);
353 Doc.addAttribute("name", Node->getLabel()->getName());
354}
355
356void StmtXML::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
357 DumpExpr(Node);
358 DumpTypeExpr(Node->getArgType1());
359 DumpTypeExpr(Node->getArgType2());
360}
361
362//===----------------------------------------------------------------------===//
363// C++ Expressions
364//===----------------------------------------------------------------------===//
365
366void StmtXML::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
367 DumpExpr(Node);
368 Doc.addAttribute("kind", Node->getCastName());
369 DumpTypeExpr(Node->getTypeAsWritten());
370}
371
372void StmtXML::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
373 DumpExpr(Node);
374 Doc.addAttribute("value", Node->getValue() ? "true" : "false");
375}
376
377void StmtXML::VisitCXXThisExpr(CXXThisExpr *Node) {
378 DumpExpr(Node);
379}
380
381void StmtXML::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
382 DumpExpr(Node);
383 DumpTypeExpr(Node->getTypeAsWritten());
384}
385
386//===----------------------------------------------------------------------===//
387// Obj-C Expressions
388//===----------------------------------------------------------------------===//
389
390void StmtXML::VisitObjCMessageExpr(ObjCMessageExpr* Node) {
391 DumpExpr(Node);
392 Doc.addAttribute("selector", Node->getSelector().getAsString());
393 IdentifierInfo* clsName = Node->getClassName();
Mike Stump1eb44332009-09-09 15:08:12 +0000394 if (clsName)
Douglas Gregoree75c052009-05-21 20:55:50 +0000395 Doc.addAttribute("class", clsName->getName());
396}
397
398void StmtXML::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
399 DumpExpr(Node);
400 DumpTypeExpr(Node->getEncodedType());
401}
402
403void StmtXML::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
404 DumpExpr(Node);
405 Doc.addAttribute("selector", Node->getSelector().getAsString());
406}
407
408void StmtXML::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
409 DumpExpr(Node);
410 Doc.addAttribute("protocol", Node->getProtocol()->getNameAsString());
411}
412
413void StmtXML::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
414 DumpExpr(Node);
415 Doc.addAttribute("property", Node->getProperty()->getNameAsString());
416}
417
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000418void StmtXML::VisitObjCImplicitSetterGetterRefExpr(
419 ObjCImplicitSetterGetterRefExpr *Node) {
Douglas Gregoree75c052009-05-21 20:55:50 +0000420 DumpExpr(Node);
421 ObjCMethodDecl *Getter = Node->getGetterMethod();
422 ObjCMethodDecl *Setter = Node->getSetterMethod();
423 Doc.addAttribute("Getter", Getter->getSelector().getAsString());
424 Doc.addAttribute("Setter", Setter ? Setter->getSelector().getAsString().c_str() : "(null)");
425}
426
427void StmtXML::VisitObjCSuperExpr(ObjCSuperExpr *Node) {
428 DumpExpr(Node);
429 Doc.addAttribute("super", "1");
430}
431
432void StmtXML::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
433 DumpExpr(Node);
434 Doc.addAttribute("kind", Node->getDecl()->getDeclKindName());
435 Doc.addAttribute("decl", Node->getDecl()->getNameAsString());
436 if (Node->isFreeIvar())
437 Doc.addAttribute("isFreeIvar", "1");
438}
Douglas Gregor038f75a2009-06-15 19:02:54 +0000439#endif
Douglas Gregoree75c052009-05-21 20:55:50 +0000440//===----------------------------------------------------------------------===//
441// Stmt method implementations
442//===----------------------------------------------------------------------===//
443
444/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
445void DocumentXML::PrintStmt(const Stmt *S) {
446 StmtXML P(*this);
447 P.DumpSubTree(const_cast<Stmt*>(S));
448}
449