blob: 31cffd0be9d8925c2a440e396f0edda9491186cf [file] [log] [blame]
Douglas Gregoree75c052009-05-21 20:55:50 +00001//===--- DocumentXML.h - XML document for ASTs ------------------*- C++ -*-===//
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 Gregoree75c052009-05-21 20:55:50 +000011// dump out the AST in a XML form that exposes type details and other fields.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_FRONTEND_DOCUMENTXML_H
16#define LLVM_CLANG_FRONTEND_DOCUMENTXML_H
17
18#include <string>
19#include <map>
Douglas Gregor038f75a2009-06-15 19:02:54 +000020#include <stack>
Douglas Gregoree75c052009-05-21 20:55:50 +000021#include "clang/AST/Type.h"
22#include "clang/AST/TypeOrdering.h"
23#include "llvm/Support/raw_ostream.h"
24#include "llvm/ADT/DenseMap.h"
25
26namespace clang {
27
28//--------------------------------------------------------- forwards
29class DeclContext;
30class Decl;
31class NamedDecl;
32class FunctionDecl;
33class ASTContext;
Douglas Gregor038f75a2009-06-15 19:02:54 +000034class LabelStmt;
Mike Stump1eb44332009-09-09 15:08:12 +000035
36//---------------------------------------------------------
37namespace XML {
Douglas Gregoree75c052009-05-21 20:55:50 +000038 // id maps:
39 template<class T>
40 struct IdMap : llvm::DenseMap<T, unsigned> {};
41
42 template<>
43 struct IdMap<QualType> : std::map<QualType, unsigned, QualTypeOrdering> {};
44
45 template<>
46 struct IdMap<std::string> : std::map<std::string, unsigned> {};
47}
48
Mike Stump1eb44332009-09-09 15:08:12 +000049//---------------------------------------------------------
50class DocumentXML {
Douglas Gregoree75c052009-05-21 20:55:50 +000051public:
52 DocumentXML(const std::string& rootName, llvm::raw_ostream& out);
Douglas Gregoree75c052009-05-21 20:55:50 +000053
54 void initialize(ASTContext &Context);
55 void PrintDecl(Decl *D);
56 void PrintStmt(const Stmt *S); // defined in StmtXML.cpp
Douglas Gregoree75c052009-05-21 20:55:50 +000057 void finalize();
58
59
60 DocumentXML& addSubNode(const std::string& name); // also enters the sub node, returns *this
61 DocumentXML& toParent(); // returns *this
62
Mike Stump1eb44332009-09-09 15:08:12 +000063 void addAttribute(const char* pName, const QualType& pType);
Douglas Gregor038f75a2009-06-15 19:02:54 +000064 void addAttribute(const char* pName, bool value);
65
66 template<class T>
Mike Stump1eb44332009-09-09 15:08:12 +000067 void addAttribute(const char* pName, const T* value) {
Douglas Gregor038f75a2009-06-15 19:02:54 +000068 addPtrAttribute(pName, value);
69 }
70
71 template<class T>
Mike Stump1eb44332009-09-09 15:08:12 +000072 void addAttribute(const char* pName, T* value) {
Douglas Gregor038f75a2009-06-15 19:02:54 +000073 addPtrAttribute(pName, value);
74 }
75
Douglas Gregoree75c052009-05-21 20:55:50 +000076 template<class T>
77 void addAttribute(const char* pName, const T& value);
Mike Stump1eb44332009-09-09 15:08:12 +000078
Douglas Gregor038f75a2009-06-15 19:02:54 +000079 template<class T>
80 void addAttributeOptional(const char* pName, const T& value);
Douglas Gregoree75c052009-05-21 20:55:50 +000081
Douglas Gregoree75c052009-05-21 20:55:50 +000082 void addSourceFileAttribute(const std::string& fileName);
83
84 PresumedLoc addLocation(const SourceLocation& Loc);
85 void addLocationRange(const SourceRange& R);
86
87 static std::string escapeString(const char* pStr, std::string::size_type len);
88
89private:
90 DocumentXML(const DocumentXML&); // not defined
91 DocumentXML& operator=(const DocumentXML&); // not defined
92
Douglas Gregor038f75a2009-06-15 19:02:54 +000093 std::stack<std::string> NodeStack;
Douglas Gregoree75c052009-05-21 20:55:50 +000094 llvm::raw_ostream& Out;
95 ASTContext *Ctx;
Douglas Gregoree75c052009-05-21 20:55:50 +000096 bool HasCurrentNodeSubNodes;
97
98
99 XML::IdMap<QualType> Types;
100 XML::IdMap<const DeclContext*> Contexts;
101 XML::IdMap<const Type*> BasicTypes;
102 XML::IdMap<std::string> SourceFiles;
103 XML::IdMap<const NamedDecl*> Decls;
Douglas Gregor038f75a2009-06-15 19:02:54 +0000104 XML::IdMap<const LabelStmt*> Labels;
Douglas Gregoree75c052009-05-21 20:55:50 +0000105
106 void addContextsRecursively(const DeclContext *DC);
Douglas Gregor038f75a2009-06-15 19:02:54 +0000107 void addTypeRecursively(const Type* pType);
Douglas Gregoree75c052009-05-21 20:55:50 +0000108 void addTypeRecursively(const QualType& pType);
109
Douglas Gregoree75c052009-05-21 20:55:50 +0000110 void Indent();
Douglas Gregor038f75a2009-06-15 19:02:54 +0000111
112 // forced pointer dispatch:
Mike Stump1eb44332009-09-09 15:08:12 +0000113 void addPtrAttribute(const char* pName, const Type* pType);
Douglas Gregor038f75a2009-06-15 19:02:54 +0000114 void addPtrAttribute(const char* pName, const NamedDecl* D);
115 void addPtrAttribute(const char* pName, const DeclContext* D);
116 void addPtrAttribute(const char* pName, const NamespaceDecl* D); // disambiguation
117 void addPtrAttribute(const char* pName, const LabelStmt* L);
118 void addPtrAttribute(const char* pName, const char* text);
119
120 // defined in TypeXML.cpp:
121 void addParentTypes(const Type* pType);
122 void writeTypeToXML(const Type* pType);
123 void writeTypeToXML(const QualType& pType);
124 class TypeAdder;
125 friend class TypeAdder;
126
127 // defined in DeclXML.cpp:
128 void writeDeclToXML(Decl *D);
129 class DeclPrinter;
130 friend class DeclPrinter;
131
132 // for addAttributeOptional:
133 static bool isDefault(unsigned value) { return value == 0; }
134 static bool isDefault(bool value) { return !value; }
135 static bool isDefault(const std::string& value) { return value.empty(); }
Douglas Gregoree75c052009-05-21 20:55:50 +0000136};
137
138//--------------------------------------------------------- inlines
139
Mike Stump1eb44332009-09-09 15:08:12 +0000140inline void DocumentXML::initialize(ASTContext &Context) {
141 Ctx = &Context;
Douglas Gregoree75c052009-05-21 20:55:50 +0000142}
143
Mike Stump1eb44332009-09-09 15:08:12 +0000144//---------------------------------------------------------
Douglas Gregoree75c052009-05-21 20:55:50 +0000145template<class T>
Mike Stump1eb44332009-09-09 15:08:12 +0000146inline void DocumentXML::addAttribute(const char* pName, const T& value) {
Douglas Gregoree75c052009-05-21 20:55:50 +0000147 Out << ' ' << pName << "=\"" << value << "\"";
148}
149
Mike Stump1eb44332009-09-09 15:08:12 +0000150//---------------------------------------------------------
151inline void DocumentXML::addPtrAttribute(const char* pName, const char* text) {
Douglas Gregor038f75a2009-06-15 19:02:54 +0000152 Out << ' ' << pName << "=\"" << text << "\"";
153}
154
Mike Stump1eb44332009-09-09 15:08:12 +0000155//---------------------------------------------------------
156inline void DocumentXML::addAttribute(const char* pName, bool value) {
Douglas Gregor038f75a2009-06-15 19:02:54 +0000157 addPtrAttribute(pName, value ? "1" : "0");
158}
159
Mike Stump1eb44332009-09-09 15:08:12 +0000160//---------------------------------------------------------
Douglas Gregor038f75a2009-06-15 19:02:54 +0000161template<class T>
Mike Stump1eb44332009-09-09 15:08:12 +0000162inline void DocumentXML::addAttributeOptional(const char* pName,
163 const T& value) {
164 if (!isDefault(value)) {
Douglas Gregor038f75a2009-06-15 19:02:54 +0000165 addAttribute(pName, value);
166 }
167}
168
Mike Stump1eb44332009-09-09 15:08:12 +0000169//---------------------------------------------------------
Douglas Gregoree75c052009-05-21 20:55:50 +0000170
Mike Stump1eb44332009-09-09 15:08:12 +0000171} //namespace clang
Douglas Gregoree75c052009-05-21 20:55:50 +0000172
173#endif //LLVM_CLANG_DOCUMENTXML_H