Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 1 | //===--- 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 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/DeclVisitor.h" |
| 17 | #include "clang/AST/Expr.h" |
| 18 | |
| 19 | namespace clang { |
| 20 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 21 | //--------------------------------------------------------- |
| 22 | class DocumentXML::DeclPrinter : public DeclVisitor<DocumentXML::DeclPrinter> { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 23 | DocumentXML& Doc; |
| 24 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 25 | void addSubNodes(FunctionDecl* FD) { |
| 26 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 27 | Visit(FD->getParamDecl(i)); |
| 28 | Doc.toParent(); |
| 29 | } |
| 30 | } |
| 31 | |
Douglas Gregor | 07425c9 | 2010-03-08 18:51:03 +0000 | [diff] [blame] | 32 | void addFunctionBody(FunctionDecl* FD) { |
| 33 | if (FD->isThisDeclarationADefinition()) { |
| 34 | Doc.addSubNode("Body"); |
| 35 | Doc.PrintStmt(FD->getBody()); |
| 36 | Doc.toParent(); |
| 37 | } |
| 38 | } |
| 39 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 40 | void addSubNodes(RecordDecl* RD) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 41 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 42 | e = RD->field_end(); i != e; ++i) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 43 | Visit(*i); |
| 44 | Doc.toParent(); |
| 45 | } |
| 46 | } |
| 47 | |
Douglas Gregor | 07425c9 | 2010-03-08 18:51:03 +0000 | [diff] [blame] | 48 | void addSubNodes(CXXRecordDecl* RD) { |
| 49 | addSubNodes(cast<RecordDecl>(RD)); |
| 50 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 51 | e = RD->method_end(); i != e; ++i) { |
| 52 | Visit(*i); |
| 53 | Doc.toParent(); |
| 54 | } |
| 55 | } |
| 56 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 | void addSubNodes(EnumDecl* ED) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 58 | for (EnumDecl::enumerator_iterator i = ED->enumerator_begin(), |
| 59 | e = ED->enumerator_end(); i != e; ++i) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 60 | Visit(*i); |
| 61 | Doc.toParent(); |
| 62 | } |
| 63 | } |
| 64 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | void addSubNodes(EnumConstantDecl* ECD) { |
| 66 | if (ECD->getInitExpr()) |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 67 | Doc.PrintStmt(ECD->getInitExpr()); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 70 | void addSubNodes(FieldDecl* FdD) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 71 | if (FdD->isBitField()) |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 72 | Doc.PrintStmt(FdD->getBitWidth()); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | void addSubNodes(VarDecl* V) { |
| 76 | if (V->getInit()) |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 77 | Doc.PrintStmt(V->getInit()); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | void addSubNodes(ParmVarDecl* argDecl) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 81 | if (argDecl->getDefaultArg()) |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 82 | Doc.PrintStmt(argDecl->getDefaultArg()); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | void addSpecialAttribute(const char* pName, EnumDecl* ED) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 86 | const QualType& enumType = ED->getIntegerType(); |
| 87 | if (!enumType.isNull()) |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 88 | Doc.addAttribute(pName, enumType); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | void addIdAttribute(LinkageSpecDecl* ED) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 92 | Doc.addAttribute("id", ED); |
| 93 | } |
| 94 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | void addIdAttribute(NamedDecl* ND) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 96 | Doc.addAttribute("id", ND); |
| 97 | } |
| 98 | |
| 99 | public: |
| 100 | DeclPrinter(DocumentXML& doc) : Doc(doc) {} |
| 101 | |
| 102 | #define NODE_XML( CLASS, NAME ) \ |
| 103 | void Visit##CLASS(CLASS* T) \ |
| 104 | { \ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | Doc.addSubNode(NAME); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 106 | |
| 107 | #define ID_ATTRIBUTE_XML addIdAttribute(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | #define ATTRIBUTE_XML( FN, NAME ) Doc.addAttribute(NAME, T->FN); |
| 109 | #define ATTRIBUTE_OPT_XML( FN, NAME ) Doc.addAttributeOptional(NAME, T->FN); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 110 | #define ATTRIBUTE_FILE_LOCATION_XML Doc.addLocation(T->getLocation()); |
| 111 | #define ATTRIBUTE_SPECIAL_XML( FN, NAME ) addSpecialAttribute(NAME, T); |
| 112 | |
| 113 | #define ATTRIBUTE_ENUM_XML( FN, NAME ) \ |
| 114 | { \ |
| 115 | const char* pAttributeName = NAME; \ |
| 116 | const bool optional = false; \ |
| 117 | switch (T->FN) { \ |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 118 | default: assert(0 && "unknown enum value"); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 119 | |
| 120 | #define ATTRIBUTE_ENUM_OPT_XML( FN, NAME ) \ |
| 121 | { \ |
| 122 | const char* pAttributeName = NAME; \ |
| 123 | const bool optional = true; \ |
| 124 | switch (T->FN) { \ |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 125 | default: assert(0 && "unknown enum value"); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 126 | |
| 127 | #define ENUM_XML( VALUE, NAME ) case VALUE: if ((!optional) || NAME[0]) Doc.addAttribute(pAttributeName, NAME); break; |
| 128 | #define END_ENUM_XML } } |
| 129 | #define END_NODE_XML } |
| 130 | |
| 131 | #define SUB_NODE_XML( CLASS ) addSubNodes(T); |
| 132 | #define SUB_NODE_SEQUENCE_XML( CLASS ) addSubNodes(T); |
| 133 | #define SUB_NODE_OPT_XML( CLASS ) addSubNodes(T); |
| 134 | |
Douglas Gregor | 07425c9 | 2010-03-08 18:51:03 +0000 | [diff] [blame] | 135 | #define SUB_NODE_FN_BODY_XML addFunctionBody(T); |
| 136 | |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 137 | #include "clang/Frontend/DeclXML.def" |
| 138 | }; |
| 139 | |
| 140 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | //--------------------------------------------------------- |
| 142 | void DocumentXML::writeDeclToXML(Decl *D) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 143 | DeclPrinter(*this).Visit(D); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 144 | toParent(); |
| 145 | } |
| 146 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | //--------------------------------------------------------- |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 148 | } // NS clang |
| 149 | |