blob: b981fc41daa948e7653900dfc926da81c386732b [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
Mike Stump1eb44332009-09-09 15:08:12 +000032 void addSubNodes(RecordDecl* RD) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000033 for (RecordDecl::field_iterator i = RD->field_begin(),
34 e = RD->field_end(); i != e; ++i) {
Douglas Gregor038f75a2009-06-15 19:02:54 +000035 Visit(*i);
36 Doc.toParent();
37 }
38 }
39
Mike Stump1eb44332009-09-09 15:08:12 +000040 void addSubNodes(EnumDecl* ED) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000041 for (EnumDecl::enumerator_iterator i = ED->enumerator_begin(),
42 e = ED->enumerator_end(); i != e; ++i) {
Douglas Gregor038f75a2009-06-15 19:02:54 +000043 Visit(*i);
44 Doc.toParent();
45 }
46 }
47
Mike Stump1eb44332009-09-09 15:08:12 +000048 void addSubNodes(EnumConstantDecl* ECD) {
49 if (ECD->getInitExpr())
Douglas Gregor038f75a2009-06-15 19:02:54 +000050 Doc.PrintStmt(ECD->getInitExpr());
Douglas Gregor038f75a2009-06-15 19:02:54 +000051 }
52
Mike Stump1eb44332009-09-09 15:08:12 +000053 void addSubNodes(FieldDecl* FdD) {
Douglas Gregor038f75a2009-06-15 19:02:54 +000054 if (FdD->isBitField())
Douglas Gregor038f75a2009-06-15 19:02:54 +000055 Doc.PrintStmt(FdD->getBitWidth());
Douglas Gregor038f75a2009-06-15 19:02:54 +000056 }
57
Mike Stump1eb44332009-09-09 15:08:12 +000058 void addSubNodes(VarDecl* V) {
59 if (V->getInit())
Douglas Gregor038f75a2009-06-15 19:02:54 +000060 Doc.PrintStmt(V->getInit());
Douglas Gregor038f75a2009-06-15 19:02:54 +000061 }
62
Mike Stump1eb44332009-09-09 15:08:12 +000063 void addSubNodes(ParmVarDecl* argDecl) {
Douglas Gregor038f75a2009-06-15 19:02:54 +000064 if (argDecl->getDefaultArg())
Douglas Gregor038f75a2009-06-15 19:02:54 +000065 Doc.PrintStmt(argDecl->getDefaultArg());
Douglas Gregor038f75a2009-06-15 19:02:54 +000066 }
67
Mike Stump1eb44332009-09-09 15:08:12 +000068 void addSpecialAttribute(const char* pName, EnumDecl* ED) {
Douglas Gregor038f75a2009-06-15 19:02:54 +000069 const QualType& enumType = ED->getIntegerType();
70 if (!enumType.isNull())
Douglas Gregor038f75a2009-06-15 19:02:54 +000071 Doc.addAttribute(pName, enumType);
Douglas Gregor038f75a2009-06-15 19:02:54 +000072 }
73
Mike Stump1eb44332009-09-09 15:08:12 +000074 void addIdAttribute(LinkageSpecDecl* ED) {
Douglas Gregor038f75a2009-06-15 19:02:54 +000075 Doc.addAttribute("id", ED);
76 }
77
Mike Stump1eb44332009-09-09 15:08:12 +000078 void addIdAttribute(NamedDecl* ND) {
Douglas Gregor038f75a2009-06-15 19:02:54 +000079 Doc.addAttribute("id", ND);
80 }
81
82public:
83 DeclPrinter(DocumentXML& doc) : Doc(doc) {}
84
85#define NODE_XML( CLASS, NAME ) \
86 void Visit##CLASS(CLASS* T) \
87 { \
Mike Stump1eb44332009-09-09 15:08:12 +000088 Doc.addSubNode(NAME);
Douglas Gregor038f75a2009-06-15 19:02:54 +000089
90#define ID_ATTRIBUTE_XML addIdAttribute(T);
Mike Stump1eb44332009-09-09 15:08:12 +000091#define ATTRIBUTE_XML( FN, NAME ) Doc.addAttribute(NAME, T->FN);
92#define ATTRIBUTE_OPT_XML( FN, NAME ) Doc.addAttributeOptional(NAME, T->FN);
Douglas Gregor038f75a2009-06-15 19:02:54 +000093#define ATTRIBUTE_FILE_LOCATION_XML Doc.addLocation(T->getLocation());
94#define ATTRIBUTE_SPECIAL_XML( FN, NAME ) addSpecialAttribute(NAME, T);
95
96#define ATTRIBUTE_ENUM_XML( FN, NAME ) \
97 { \
98 const char* pAttributeName = NAME; \
99 const bool optional = false; \
100 switch (T->FN) { \
Mike Stump1eb44332009-09-09 15:08:12 +0000101 default: assert(0 && "unknown enum value");
Douglas Gregor038f75a2009-06-15 19:02:54 +0000102
103#define ATTRIBUTE_ENUM_OPT_XML( FN, NAME ) \
104 { \
105 const char* pAttributeName = NAME; \
106 const bool optional = true; \
107 switch (T->FN) { \
Mike Stump1eb44332009-09-09 15:08:12 +0000108 default: assert(0 && "unknown enum value");
Douglas Gregor038f75a2009-06-15 19:02:54 +0000109
110#define ENUM_XML( VALUE, NAME ) case VALUE: if ((!optional) || NAME[0]) Doc.addAttribute(pAttributeName, NAME); break;
111#define END_ENUM_XML } }
112#define END_NODE_XML }
113
114#define SUB_NODE_XML( CLASS ) addSubNodes(T);
115#define SUB_NODE_SEQUENCE_XML( CLASS ) addSubNodes(T);
116#define SUB_NODE_OPT_XML( CLASS ) addSubNodes(T);
117
118#include "clang/Frontend/DeclXML.def"
119};
120
121
Mike Stump1eb44332009-09-09 15:08:12 +0000122//---------------------------------------------------------
123void DocumentXML::writeDeclToXML(Decl *D) {
Douglas Gregor038f75a2009-06-15 19:02:54 +0000124 DeclPrinter(*this).Visit(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000125 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000126 if (Stmt *Body = FD->getBody()) {
Douglas Gregor038f75a2009-06-15 19:02:54 +0000127 addSubNode("Body");
128 PrintStmt(Body);
129 toParent();
130 }
131 }
132 toParent();
133}
134
Mike Stump1eb44332009-09-09 15:08:12 +0000135//---------------------------------------------------------
Douglas Gregor038f75a2009-06-15 19:02:54 +0000136} // NS clang
137