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)); |
Douglas Gregor | 4bd98e8 | 2010-05-10 17:43:18 +0000 | [diff] [blame] | 50 | |
| 51 | if (RD->isDefinition()) { |
Chris Lattner | edd8df9 | 2010-05-12 23:27:11 +0000 | [diff] [blame] | 52 | // FIXME: This breaks XML generation |
| 53 | //Doc.addAttribute("num_bases", RD->getNumBases()); |
Douglas Gregor | 4bd98e8 | 2010-05-10 17:43:18 +0000 | [diff] [blame] | 54 | |
| 55 | for (CXXRecordDecl::base_class_iterator |
| 56 | base = RD->bases_begin(), |
| 57 | bend = RD->bases_end(); |
| 58 | base != bend; |
| 59 | ++base) { |
| 60 | Doc.addSubNode("Base"); |
| 61 | Doc.addAttribute("id", base->getType()); |
| 62 | AccessSpecifier as = base->getAccessSpecifierAsWritten(); |
| 63 | const char* as_name = ""; |
| 64 | switch(as) { |
| 65 | case AS_none: as_name = ""; break; |
| 66 | case AS_public: as_name = "public"; break; |
| 67 | case AS_protected: as_name = "protected"; break; |
| 68 | case AS_private: as_name = "private"; break; |
| 69 | } |
| 70 | Doc.addAttributeOptional("access", as_name); |
| 71 | Doc.addAttribute("is_virtual", base->isVirtual()); |
| 72 | Doc.toParent(); |
| 73 | } |
| 74 | |
| 75 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 76 | e = RD->method_end(); i != e; ++i) { |
| 77 | Visit(*i); |
| 78 | Doc.toParent(); |
| 79 | } |
| 80 | |
Douglas Gregor | 07425c9 | 2010-03-08 18:51:03 +0000 | [diff] [blame] | 81 | } |
Douglas Gregor | 4bd98e8 | 2010-05-10 17:43:18 +0000 | [diff] [blame] | 82 | |
Douglas Gregor | 07425c9 | 2010-03-08 18:51:03 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | void addSubNodes(EnumDecl* ED) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 86 | for (EnumDecl::enumerator_iterator i = ED->enumerator_begin(), |
| 87 | e = ED->enumerator_end(); i != e; ++i) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 88 | Visit(*i); |
| 89 | Doc.toParent(); |
| 90 | } |
| 91 | } |
| 92 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | void addSubNodes(EnumConstantDecl* ECD) { |
| 94 | if (ECD->getInitExpr()) |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 95 | Doc.PrintStmt(ECD->getInitExpr()); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | void addSubNodes(FieldDecl* FdD) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 99 | if (FdD->isBitField()) |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 100 | Doc.PrintStmt(FdD->getBitWidth()); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | void addSubNodes(VarDecl* V) { |
| 104 | if (V->getInit()) |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 105 | Doc.PrintStmt(V->getInit()); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | void addSubNodes(ParmVarDecl* argDecl) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 109 | if (argDecl->getDefaultArg()) |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 110 | Doc.PrintStmt(argDecl->getDefaultArg()); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Douglas Gregor | 4bd98e8 | 2010-05-10 17:43:18 +0000 | [diff] [blame] | 113 | void addSubNodes(NamespaceDecl* ns) { |
| 114 | |
| 115 | for (DeclContext::decl_iterator |
| 116 | d = ns->decls_begin(), |
| 117 | dend = ns->decls_end(); |
| 118 | d != dend; |
| 119 | ++d) { |
| 120 | Visit(*d); |
| 121 | Doc.toParent(); |
| 122 | } |
| 123 | } |
| 124 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | void addSpecialAttribute(const char* pName, EnumDecl* ED) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 126 | const QualType& enumType = ED->getIntegerType(); |
| 127 | if (!enumType.isNull()) |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 128 | Doc.addAttribute(pName, enumType); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | void addIdAttribute(LinkageSpecDecl* ED) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 132 | Doc.addAttribute("id", ED); |
| 133 | } |
| 134 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | void addIdAttribute(NamedDecl* ND) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 136 | Doc.addAttribute("id", ND); |
| 137 | } |
| 138 | |
| 139 | public: |
| 140 | DeclPrinter(DocumentXML& doc) : Doc(doc) {} |
| 141 | |
| 142 | #define NODE_XML( CLASS, NAME ) \ |
| 143 | void Visit##CLASS(CLASS* T) \ |
| 144 | { \ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 145 | Doc.addSubNode(NAME); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 146 | |
| 147 | #define ID_ATTRIBUTE_XML addIdAttribute(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 148 | #define ATTRIBUTE_XML( FN, NAME ) Doc.addAttribute(NAME, T->FN); |
| 149 | #define ATTRIBUTE_OPT_XML( FN, NAME ) Doc.addAttributeOptional(NAME, T->FN); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 150 | #define ATTRIBUTE_FILE_LOCATION_XML Doc.addLocation(T->getLocation()); |
| 151 | #define ATTRIBUTE_SPECIAL_XML( FN, NAME ) addSpecialAttribute(NAME, T); |
| 152 | |
| 153 | #define ATTRIBUTE_ENUM_XML( FN, NAME ) \ |
| 154 | { \ |
| 155 | const char* pAttributeName = NAME; \ |
| 156 | const bool optional = false; \ |
| 157 | switch (T->FN) { \ |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 158 | default: assert(0 && "unknown enum value"); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 159 | |
| 160 | #define ATTRIBUTE_ENUM_OPT_XML( FN, NAME ) \ |
| 161 | { \ |
| 162 | const char* pAttributeName = NAME; \ |
| 163 | const bool optional = true; \ |
| 164 | switch (T->FN) { \ |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 165 | default: assert(0 && "unknown enum value"); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 166 | |
| 167 | #define ENUM_XML( VALUE, NAME ) case VALUE: if ((!optional) || NAME[0]) Doc.addAttribute(pAttributeName, NAME); break; |
| 168 | #define END_ENUM_XML } } |
| 169 | #define END_NODE_XML } |
| 170 | |
| 171 | #define SUB_NODE_XML( CLASS ) addSubNodes(T); |
| 172 | #define SUB_NODE_SEQUENCE_XML( CLASS ) addSubNodes(T); |
| 173 | #define SUB_NODE_OPT_XML( CLASS ) addSubNodes(T); |
| 174 | |
Douglas Gregor | 07425c9 | 2010-03-08 18:51:03 +0000 | [diff] [blame] | 175 | #define SUB_NODE_FN_BODY_XML addFunctionBody(T); |
| 176 | |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 177 | #include "clang/Frontend/DeclXML.def" |
| 178 | }; |
| 179 | |
| 180 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 181 | //--------------------------------------------------------- |
| 182 | void DocumentXML::writeDeclToXML(Decl *D) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 183 | DeclPrinter(*this).Visit(D); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 184 | toParent(); |
| 185 | } |
| 186 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | //--------------------------------------------------------- |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 188 | } // NS clang |
| 189 | |