blob: 97a7f5558392534ca1edc1c57f747e6c8fdd8697 [file] [log] [blame]
Douglas Gregor038f75a2009-06-15 19:02:54 +00001//===--- DeclXML.cpp - XML implementation for Decl 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//
Mike Stump1eb44332009-09-09 15:08:12 +000010// This file implements the XML document class, which provides the means to
Douglas Gregor038f75a2009-06-15 19:02:54 +000011// dump out the AST in a XML form that exposes type details and other fields.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Frontend/DocumentXML.h"
16#include "clang/AST/DeclVisitor.h"
17#include "clang/AST/Expr.h"
18
19namespace clang {
20
Mike Stump1eb44332009-09-09 15:08:12 +000021//---------------------------------------------------------
22class DocumentXML::DeclPrinter : public DeclVisitor<DocumentXML::DeclPrinter> {
Douglas Gregor038f75a2009-06-15 19:02:54 +000023 DocumentXML& Doc;
24
Mike Stump1eb44332009-09-09 15:08:12 +000025 void addSubNodes(FunctionDecl* FD) {
26 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
Douglas Gregor038f75a2009-06-15 19:02:54 +000027 Visit(FD->getParamDecl(i));
28 Doc.toParent();
29 }
30 }
31
Douglas Gregor07425c92010-03-08 18:51:03 +000032 void addFunctionBody(FunctionDecl* FD) {
33 if (FD->isThisDeclarationADefinition()) {
34 Doc.addSubNode("Body");
35 Doc.PrintStmt(FD->getBody());
36 Doc.toParent();
37 }
38 }
39
Mike Stump1eb44332009-09-09 15:08:12 +000040 void addSubNodes(RecordDecl* RD) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000041 for (RecordDecl::field_iterator i = RD->field_begin(),
42 e = RD->field_end(); i != e; ++i) {
Douglas Gregor038f75a2009-06-15 19:02:54 +000043 Visit(*i);
44 Doc.toParent();
45 }
46 }
47
Douglas Gregor07425c92010-03-08 18:51:03 +000048 void addSubNodes(CXXRecordDecl* RD) {
49 addSubNodes(cast<RecordDecl>(RD));
Douglas Gregor4bd98e82010-05-10 17:43:18 +000050
51 if (RD->isDefinition()) {
Chris Lattneredd8df92010-05-12 23:27:11 +000052 // FIXME: This breaks XML generation
53 //Doc.addAttribute("num_bases", RD->getNumBases());
Douglas Gregor4bd98e82010-05-10 17:43:18 +000054
55 for (CXXRecordDecl::base_class_iterator
56 base = RD->bases_begin(),
57 bend = RD->bases_end();
58 base != bend;
59 ++base) {
60 Doc.addSubNode("Base");
61 Doc.addAttribute("id", base->getType());
62 AccessSpecifier as = base->getAccessSpecifierAsWritten();
63 const char* as_name = "";
64 switch(as) {
65 case AS_none: as_name = ""; break;
66 case AS_public: as_name = "public"; break;
67 case AS_protected: as_name = "protected"; break;
68 case AS_private: as_name = "private"; break;
69 }
70 Doc.addAttributeOptional("access", as_name);
71 Doc.addAttribute("is_virtual", base->isVirtual());
72 Doc.toParent();
73 }
74
75 for (CXXRecordDecl::method_iterator i = RD->method_begin(),
76 e = RD->method_end(); i != e; ++i) {
77 Visit(*i);
78 Doc.toParent();
79 }
80
Douglas Gregor07425c92010-03-08 18:51:03 +000081 }
Douglas Gregor4bd98e82010-05-10 17:43:18 +000082
Douglas Gregor07425c92010-03-08 18:51:03 +000083 }
84
Mike Stump1eb44332009-09-09 15:08:12 +000085 void addSubNodes(EnumDecl* ED) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000086 for (EnumDecl::enumerator_iterator i = ED->enumerator_begin(),
87 e = ED->enumerator_end(); i != e; ++i) {
Douglas Gregor038f75a2009-06-15 19:02:54 +000088 Visit(*i);
89 Doc.toParent();
90 }
91 }
92
Mike Stump1eb44332009-09-09 15:08:12 +000093 void addSubNodes(EnumConstantDecl* ECD) {
94 if (ECD->getInitExpr())
Douglas Gregor038f75a2009-06-15 19:02:54 +000095 Doc.PrintStmt(ECD->getInitExpr());
Douglas Gregor038f75a2009-06-15 19:02:54 +000096 }
97
Mike Stump1eb44332009-09-09 15:08:12 +000098 void addSubNodes(FieldDecl* FdD) {
Douglas Gregor038f75a2009-06-15 19:02:54 +000099 if (FdD->isBitField())
Douglas Gregor038f75a2009-06-15 19:02:54 +0000100 Doc.PrintStmt(FdD->getBitWidth());
Douglas Gregor038f75a2009-06-15 19:02:54 +0000101 }
102
Mike Stump1eb44332009-09-09 15:08:12 +0000103 void addSubNodes(VarDecl* V) {
104 if (V->getInit())
Douglas Gregor038f75a2009-06-15 19:02:54 +0000105 Doc.PrintStmt(V->getInit());
Douglas Gregor038f75a2009-06-15 19:02:54 +0000106 }
107
Mike Stump1eb44332009-09-09 15:08:12 +0000108 void addSubNodes(ParmVarDecl* argDecl) {
Douglas Gregor038f75a2009-06-15 19:02:54 +0000109 if (argDecl->getDefaultArg())
Douglas Gregor038f75a2009-06-15 19:02:54 +0000110 Doc.PrintStmt(argDecl->getDefaultArg());
Douglas Gregor038f75a2009-06-15 19:02:54 +0000111 }
112
Douglas Gregor4bd98e82010-05-10 17:43:18 +0000113 void addSubNodes(NamespaceDecl* ns) {
114
115 for (DeclContext::decl_iterator
116 d = ns->decls_begin(),
117 dend = ns->decls_end();
118 d != dend;
119 ++d) {
120 Visit(*d);
121 Doc.toParent();
122 }
123 }
124
Mike Stump1eb44332009-09-09 15:08:12 +0000125 void addSpecialAttribute(const char* pName, EnumDecl* ED) {
Douglas Gregor038f75a2009-06-15 19:02:54 +0000126 const QualType& enumType = ED->getIntegerType();
127 if (!enumType.isNull())
Douglas Gregor038f75a2009-06-15 19:02:54 +0000128 Doc.addAttribute(pName, enumType);
Douglas Gregor038f75a2009-06-15 19:02:54 +0000129 }
130
Mike Stump1eb44332009-09-09 15:08:12 +0000131 void addIdAttribute(LinkageSpecDecl* ED) {
Douglas Gregor038f75a2009-06-15 19:02:54 +0000132 Doc.addAttribute("id", ED);
133 }
134
Mike Stump1eb44332009-09-09 15:08:12 +0000135 void addIdAttribute(NamedDecl* ND) {
Douglas Gregor038f75a2009-06-15 19:02:54 +0000136 Doc.addAttribute("id", ND);
137 }
138
139public:
140 DeclPrinter(DocumentXML& doc) : Doc(doc) {}
141
142#define NODE_XML( CLASS, NAME ) \
143 void Visit##CLASS(CLASS* T) \
144 { \
Mike Stump1eb44332009-09-09 15:08:12 +0000145 Doc.addSubNode(NAME);
Douglas Gregor038f75a2009-06-15 19:02:54 +0000146
147#define ID_ATTRIBUTE_XML addIdAttribute(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000148#define ATTRIBUTE_XML( FN, NAME ) Doc.addAttribute(NAME, T->FN);
149#define ATTRIBUTE_OPT_XML( FN, NAME ) Doc.addAttributeOptional(NAME, T->FN);
Douglas Gregor038f75a2009-06-15 19:02:54 +0000150#define ATTRIBUTE_FILE_LOCATION_XML Doc.addLocation(T->getLocation());
151#define ATTRIBUTE_SPECIAL_XML( FN, NAME ) addSpecialAttribute(NAME, T);
152
153#define ATTRIBUTE_ENUM_XML( FN, NAME ) \
154 { \
155 const char* pAttributeName = NAME; \
156 const bool optional = false; \
157 switch (T->FN) { \
Mike Stumpb7166332010-01-20 02:03:14 +0000158 default: assert(0 && "unknown enum value");
Douglas Gregor038f75a2009-06-15 19:02:54 +0000159
160#define ATTRIBUTE_ENUM_OPT_XML( FN, NAME ) \
161 { \
162 const char* pAttributeName = NAME; \
163 const bool optional = true; \
164 switch (T->FN) { \
Mike Stumpb7166332010-01-20 02:03:14 +0000165 default: assert(0 && "unknown enum value");
Douglas Gregor038f75a2009-06-15 19:02:54 +0000166
167#define ENUM_XML( VALUE, NAME ) case VALUE: if ((!optional) || NAME[0]) Doc.addAttribute(pAttributeName, NAME); break;
168#define END_ENUM_XML } }
169#define END_NODE_XML }
170
171#define SUB_NODE_XML( CLASS ) addSubNodes(T);
172#define SUB_NODE_SEQUENCE_XML( CLASS ) addSubNodes(T);
173#define SUB_NODE_OPT_XML( CLASS ) addSubNodes(T);
174
Douglas Gregor07425c92010-03-08 18:51:03 +0000175#define SUB_NODE_FN_BODY_XML addFunctionBody(T);
176
Douglas Gregor038f75a2009-06-15 19:02:54 +0000177#include "clang/Frontend/DeclXML.def"
178};
179
180
Mike Stump1eb44332009-09-09 15:08:12 +0000181//---------------------------------------------------------
182void DocumentXML::writeDeclToXML(Decl *D) {
Douglas Gregor038f75a2009-06-15 19:02:54 +0000183 DeclPrinter(*this).Visit(D);
Douglas Gregor038f75a2009-06-15 19:02:54 +0000184 toParent();
185}
186
Mike Stump1eb44332009-09-09 15:08:12 +0000187//---------------------------------------------------------
Douglas Gregor038f75a2009-06-15 19:02:54 +0000188} // NS clang
189