Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 1 | //===--- DocumentXML.cpp - XML document for 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 Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10 | // This file implements the XML document class, which provides the means to |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 11 | // 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/TypeVisitor.h" |
| 17 | #include "clang/AST/Type.h" |
| 18 | #include "clang/AST/Decl.h" |
| 19 | |
| 20 | namespace clang { |
| 21 | namespace XML { |
| 22 | namespace { |
| 23 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 24 | //--------------------------------------------------------- |
| 25 | class TypeWriter : public TypeVisitor<TypeWriter> { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 26 | DocumentXML& Doc; |
| 27 | |
| 28 | public: |
| 29 | TypeWriter(DocumentXML& doc) : Doc(doc) {} |
| 30 | |
| 31 | #define NODE_XML( CLASS, NAME ) \ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 32 | void Visit##CLASS(CLASS* T) { \ |
| 33 | Doc.addSubNode(NAME); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 34 | |
| 35 | #define ID_ATTRIBUTE_XML // done by the Document class itself |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 36 | #define ATTRIBUTE_XML( FN, NAME ) Doc.addAttribute(NAME, T->FN); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 37 | #define TYPE_ATTRIBUTE_XML( FN ) ATTRIBUTE_XML(FN, "type") |
| 38 | #define CONTEXT_ATTRIBUTE_XML( FN ) ATTRIBUTE_XML(FN, "context") |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | #define ATTRIBUTE_OPT_XML( FN, NAME ) Doc.addAttributeOptional(NAME, T->FN); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 40 | |
| 41 | #define ATTRIBUTE_ENUM_XML( FN, NAME ) \ |
| 42 | { \ |
| 43 | const char* pAttributeName = NAME; \ |
| 44 | const bool optional = false; \ |
| 45 | switch (T->FN) { \ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 | default: assert(0 && "unknown enum value"); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 47 | |
| 48 | #define ATTRIBUTE_ENUM_OPT_XML( FN, NAME ) \ |
| 49 | { \ |
| 50 | const char* pAttributeName = NAME; \ |
| 51 | const bool optional = true; \ |
| 52 | switch (T->FN) { \ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 53 | default: assert(0 && "unknown enum value"); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 54 | |
| 55 | #define ENUM_XML( VALUE, NAME ) case VALUE: if ((!optional) || NAME[0]) Doc.addAttribute(pAttributeName, NAME); break; |
| 56 | #define END_ENUM_XML } } |
| 57 | #define END_NODE_XML } |
| 58 | |
| 59 | #include "clang/Frontend/TypeXML.def" |
| 60 | |
| 61 | }; |
| 62 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | //--------------------------------------------------------- |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 64 | } // anon clang |
| 65 | } // NS XML |
| 66 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 67 | //--------------------------------------------------------- |
| 68 | class DocumentXML::TypeAdder : public TypeVisitor<DocumentXML::TypeAdder> { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 69 | DocumentXML& Doc; |
| 70 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | void addIfType(const Type* pType) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 72 | Doc.addTypeRecursively(pType); |
| 73 | } |
| 74 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | void addIfType(const QualType& pType) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 76 | Doc.addTypeRecursively(pType); |
| 77 | } |
| 78 | |
| 79 | template<class T> void addIfType(T) {} |
| 80 | |
| 81 | public: |
| 82 | TypeAdder(DocumentXML& doc) : Doc(doc) {} |
| 83 | |
| 84 | #define NODE_XML( CLASS, NAME ) \ |
| 85 | void Visit##CLASS(CLASS* T) \ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 87 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 88 | #define ID_ATTRIBUTE_XML |
| 89 | #define TYPE_ATTRIBUTE_XML( FN ) Doc.addTypeRecursively(T->FN); |
| 90 | #define CONTEXT_ATTRIBUTE_XML( FN ) |
| 91 | #define ATTRIBUTE_XML( FN, NAME ) addIfType(T->FN); |
| 92 | #define ATTRIBUTE_OPT_XML( FN, NAME ) |
| 93 | #define ATTRIBUTE_ENUM_XML( FN, NAME ) |
| 94 | #define ATTRIBUTE_ENUM_OPT_XML( FN, NAME ) |
| 95 | #define ENUM_XML( VALUE, NAME ) |
| 96 | #define END_ENUM_XML |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 97 | #define END_NODE_XML } |
| 98 | |
| 99 | #include "clang/Frontend/TypeXML.def" |
| 100 | }; |
| 101 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | //--------------------------------------------------------- |
| 103 | void DocumentXML::addParentTypes(const Type* pType) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 104 | TypeAdder(*this).Visit(const_cast<Type*>(pType)); |
| 105 | } |
| 106 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | //--------------------------------------------------------- |
| 108 | void DocumentXML::writeTypeToXML(const Type* pType) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 109 | XML::TypeWriter(*this).Visit(const_cast<Type*>(pType)); |
| 110 | } |
| 111 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | //--------------------------------------------------------- |
| 113 | void DocumentXML::writeTypeToXML(const QualType& pType) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 114 | XML::TypeWriter(*this).VisitQualType(const_cast<QualType*>(&pType)); |
| 115 | } |
| 116 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | //--------------------------------------------------------- |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 118 | } // NS clang |
| 119 | |