blob: 8d3d225a4b38bec86b58523c1c2c5fc2fbcde1e8 [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) {
Daniel Dunbar54d56a52010-09-29 16:09:28 +000041 for (RecordDecl::decl_iterator i = RD->decls_begin(),
42 e = RD->decls_end(); i != e; ++i) {
Douglas Gregor642bfaa2010-10-08 21:05:46 +000043 if (!(*i)->isImplicit()) {
44 Visit(*i);
45 Doc.toParent();
Daniel Dunbar54d56a52010-09-29 16:09:28 +000046 }
47 }
Douglas Gregor038f75a2009-06-15 19:02:54 +000048 }
49
Douglas Gregor07425c92010-03-08 18:51:03 +000050 void addSubNodes(CXXRecordDecl* RD) {
51 addSubNodes(cast<RecordDecl>(RD));
Douglas Gregor4bd98e82010-05-10 17:43:18 +000052
53 if (RD->isDefinition()) {
Chris Lattneredd8df92010-05-12 23:27:11 +000054 // FIXME: This breaks XML generation
55 //Doc.addAttribute("num_bases", RD->getNumBases());
Douglas Gregor4bd98e82010-05-10 17:43:18 +000056
57 for (CXXRecordDecl::base_class_iterator
58 base = RD->bases_begin(),
59 bend = RD->bases_end();
60 base != bend;
61 ++base) {
62 Doc.addSubNode("Base");
63 Doc.addAttribute("id", base->getType());
64 AccessSpecifier as = base->getAccessSpecifierAsWritten();
65 const char* as_name = "";
66 switch(as) {
67 case AS_none: as_name = ""; break;
68 case AS_public: as_name = "public"; break;
69 case AS_protected: as_name = "protected"; break;
70 case AS_private: as_name = "private"; break;
71 }
72 Doc.addAttributeOptional("access", as_name);
73 Doc.addAttribute("is_virtual", base->isVirtual());
74 Doc.toParent();
75 }
Douglas Gregor07425c92010-03-08 18:51:03 +000076 }
77 }
78
Mike Stump1eb44332009-09-09 15:08:12 +000079 void addSubNodes(EnumDecl* ED) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000080 for (EnumDecl::enumerator_iterator i = ED->enumerator_begin(),
81 e = ED->enumerator_end(); i != e; ++i) {
Douglas Gregor038f75a2009-06-15 19:02:54 +000082 Visit(*i);
83 Doc.toParent();
84 }
85 }
86
Mike Stump1eb44332009-09-09 15:08:12 +000087 void addSubNodes(EnumConstantDecl* ECD) {
88 if (ECD->getInitExpr())
Douglas Gregor038f75a2009-06-15 19:02:54 +000089 Doc.PrintStmt(ECD->getInitExpr());
Douglas Gregor038f75a2009-06-15 19:02:54 +000090 }
91
Mike Stump1eb44332009-09-09 15:08:12 +000092 void addSubNodes(FieldDecl* FdD) {
Douglas Gregor038f75a2009-06-15 19:02:54 +000093 if (FdD->isBitField())
Douglas Gregor038f75a2009-06-15 19:02:54 +000094 Doc.PrintStmt(FdD->getBitWidth());
Douglas Gregor038f75a2009-06-15 19:02:54 +000095 }
96
Mike Stump1eb44332009-09-09 15:08:12 +000097 void addSubNodes(VarDecl* V) {
98 if (V->getInit())
Douglas Gregor038f75a2009-06-15 19:02:54 +000099 Doc.PrintStmt(V->getInit());
Douglas Gregor038f75a2009-06-15 19:02:54 +0000100 }
101
Mike Stump1eb44332009-09-09 15:08:12 +0000102 void addSubNodes(ParmVarDecl* argDecl) {
Douglas Gregor038f75a2009-06-15 19:02:54 +0000103 if (argDecl->getDefaultArg())
Douglas Gregor038f75a2009-06-15 19:02:54 +0000104 Doc.PrintStmt(argDecl->getDefaultArg());
Douglas Gregor038f75a2009-06-15 19:02:54 +0000105 }
106
Douglas Gregorcf1123b2010-10-18 14:35:28 +0000107 void addSubNodes(DeclContext* ns) {
Douglas Gregor4bd98e82010-05-10 17:43:18 +0000108
109 for (DeclContext::decl_iterator
110 d = ns->decls_begin(),
111 dend = ns->decls_end();
112 d != dend;
113 ++d) {
114 Visit(*d);
115 Doc.toParent();
116 }
117 }
118
Mike Stump1eb44332009-09-09 15:08:12 +0000119 void addSpecialAttribute(const char* pName, EnumDecl* ED) {
Douglas Gregor038f75a2009-06-15 19:02:54 +0000120 const QualType& enumType = ED->getIntegerType();
121 if (!enumType.isNull())
Douglas Gregor038f75a2009-06-15 19:02:54 +0000122 Doc.addAttribute(pName, enumType);
Douglas Gregor038f75a2009-06-15 19:02:54 +0000123 }
124
Mike Stump1eb44332009-09-09 15:08:12 +0000125 void addIdAttribute(LinkageSpecDecl* ED) {
Douglas Gregor038f75a2009-06-15 19:02:54 +0000126 Doc.addAttribute("id", ED);
127 }
128
Mike Stump1eb44332009-09-09 15:08:12 +0000129 void addIdAttribute(NamedDecl* ND) {
Douglas Gregor038f75a2009-06-15 19:02:54 +0000130 Doc.addAttribute("id", ND);
131 }
132
133public:
134 DeclPrinter(DocumentXML& doc) : Doc(doc) {}
135
136#define NODE_XML( CLASS, NAME ) \
137 void Visit##CLASS(CLASS* T) \
138 { \
Mike Stump1eb44332009-09-09 15:08:12 +0000139 Doc.addSubNode(NAME);
Douglas Gregor038f75a2009-06-15 19:02:54 +0000140
141#define ID_ATTRIBUTE_XML addIdAttribute(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000142#define ATTRIBUTE_XML( FN, NAME ) Doc.addAttribute(NAME, T->FN);
143#define ATTRIBUTE_OPT_XML( FN, NAME ) Doc.addAttributeOptional(NAME, T->FN);
Douglas Gregor038f75a2009-06-15 19:02:54 +0000144#define ATTRIBUTE_FILE_LOCATION_XML Doc.addLocation(T->getLocation());
145#define ATTRIBUTE_SPECIAL_XML( FN, NAME ) addSpecialAttribute(NAME, T);
146
147#define ATTRIBUTE_ENUM_XML( FN, NAME ) \
148 { \
149 const char* pAttributeName = NAME; \
150 const bool optional = false; \
151 switch (T->FN) { \
Mike Stumpb7166332010-01-20 02:03:14 +0000152 default: assert(0 && "unknown enum value");
Douglas Gregor038f75a2009-06-15 19:02:54 +0000153
154#define ATTRIBUTE_ENUM_OPT_XML( FN, NAME ) \
155 { \
156 const char* pAttributeName = NAME; \
157 const bool optional = true; \
158 switch (T->FN) { \
Mike Stumpb7166332010-01-20 02:03:14 +0000159 default: assert(0 && "unknown enum value");
Douglas Gregor038f75a2009-06-15 19:02:54 +0000160
161#define ENUM_XML( VALUE, NAME ) case VALUE: if ((!optional) || NAME[0]) Doc.addAttribute(pAttributeName, NAME); break;
162#define END_ENUM_XML } }
163#define END_NODE_XML }
164
165#define SUB_NODE_XML( CLASS ) addSubNodes(T);
166#define SUB_NODE_SEQUENCE_XML( CLASS ) addSubNodes(T);
167#define SUB_NODE_OPT_XML( CLASS ) addSubNodes(T);
168
Douglas Gregor07425c92010-03-08 18:51:03 +0000169#define SUB_NODE_FN_BODY_XML addFunctionBody(T);
170
Douglas Gregor038f75a2009-06-15 19:02:54 +0000171#include "clang/Frontend/DeclXML.def"
172};
173
174
Mike Stump1eb44332009-09-09 15:08:12 +0000175//---------------------------------------------------------
176void DocumentXML::writeDeclToXML(Decl *D) {
Douglas Gregor038f75a2009-06-15 19:02:54 +0000177 DeclPrinter(*this).Visit(D);
Douglas Gregor038f75a2009-06-15 19:02:54 +0000178 toParent();
179}
180
Mike Stump1eb44332009-09-09 15:08:12 +0000181//---------------------------------------------------------
Douglas Gregor038f75a2009-06-15 19:02:54 +0000182} // NS clang
183