blob: dcbc5180a03e5700549c93f750ed0a38bd0fb8e3 [file] [log] [blame]
John McCall26c25c92010-11-24 11:30:07 +00001//===--- DumpXML.cpp - Detailed XML dumping ---------------------*- 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//
10// This file defines the Decl::dumpXML() method, a debugging tool to
11// print a detailed graph of an AST in an unspecified XML format.
12//
13// There is no guarantee of stability for this format.
14//
15//===----------------------------------------------------------------------===//
16
17// Only pay for this in code size in assertions-enabled builds.
18
19#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclCXX.h"
22#include "clang/AST/DeclFriend.h"
23#include "clang/AST/DeclObjC.h"
24#include "clang/AST/DeclTemplate.h"
25#include "clang/AST/DeclVisitor.h"
26#include "clang/AST/Expr.h"
27#include "clang/AST/ExprCXX.h"
28#include "clang/AST/ExprObjC.h"
29#include "clang/AST/NestedNameSpecifier.h"
30#include "clang/AST/Stmt.h"
31#include "clang/AST/StmtCXX.h"
32#include "clang/AST/StmtObjC.h"
33#include "clang/AST/StmtVisitor.h"
34#include "clang/AST/TemplateBase.h"
35#include "clang/AST/TemplateName.h"
36#include "clang/AST/Type.h"
37#include "clang/AST/TypeLoc.h"
38#include "clang/AST/TypeLocVisitor.h"
39#include "clang/AST/TypeVisitor.h"
40#include "clang/AST/Expr.h"
41#include "clang/AST/ExprCXX.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000042#include "llvm/ADT/SmallString.h"
John McCall26c25c92010-11-24 11:30:07 +000043
44using namespace clang;
45
46#ifndef NDEBUG
47
48namespace {
49
50enum NodeState {
51 NS_Attrs, NS_LazyChildren, NS_Children
52};
53
54struct Node {
Chris Lattner5f9e2722011-07-23 10:55:15 +000055 StringRef Name;
John McCall26c25c92010-11-24 11:30:07 +000056 NodeState State;
Chris Lattner5f9e2722011-07-23 10:55:15 +000057 Node(StringRef name) : Name(name), State(NS_Attrs) {}
John McCall26c25c92010-11-24 11:30:07 +000058
59 bool isDoneWithAttrs() const { return State != NS_Attrs; }
60};
61
62template <class Impl> struct XMLDeclVisitor {
63#define DISPATCH(NAME, CLASS) \
64 static_cast<Impl*>(this)->NAME(static_cast<CLASS*>(D))
65
66 void dispatch(Decl *D) {
67 switch (D->getKind()) {
John McCall26c25c92010-11-24 11:30:07 +000068#define DECL(DERIVED, BASE) \
69 case Decl::DERIVED: \
70 DISPATCH(dispatch##DERIVED##DeclAttrs, DERIVED##Decl); \
71 static_cast<Impl*>(this)->completeAttrs(); \
72 DISPATCH(dispatch##DERIVED##DeclChildren, DERIVED##Decl); \
73 DISPATCH(dispatch##DERIVED##DeclAsContext, DERIVED##Decl); \
74 break;
75#define ABSTRACT_DECL(DECL)
76#include "clang/AST/DeclNodes.inc"
77 }
78 }
79
80#define DECL(DERIVED, BASE) \
81 void dispatch##DERIVED##DeclAttrs(DERIVED##Decl *D) { \
82 DISPATCH(dispatch##BASE##Attrs, BASE); \
83 DISPATCH(visit##DERIVED##DeclAttrs, DERIVED##Decl); \
84 } \
85 void visit##DERIVED##DeclAttrs(DERIVED##Decl *D) {} \
86 void dispatch##DERIVED##DeclChildren(DERIVED##Decl *D) { \
87 DISPATCH(dispatch##BASE##Children, BASE); \
88 DISPATCH(visit##DERIVED##DeclChildren, DERIVED##Decl); \
89 } \
90 void visit##DERIVED##DeclChildren(DERIVED##Decl *D) {} \
91 void dispatch##DERIVED##DeclAsContext(DERIVED##Decl *D) { \
92 DISPATCH(dispatch##BASE##AsContext, BASE); \
93 DISPATCH(visit##DERIVED##DeclAsContext, DERIVED##Decl); \
94 } \
95 void visit##DERIVED##DeclAsContext(DERIVED##Decl *D) {}
96#include "clang/AST/DeclNodes.inc"
97
98 void dispatchDeclAttrs(Decl *D) {
99 DISPATCH(visitDeclAttrs, Decl);
100 }
101 void visitDeclAttrs(Decl *D) {}
102
103 void dispatchDeclChildren(Decl *D) {
104 DISPATCH(visitDeclChildren, Decl);
105 }
106 void visitDeclChildren(Decl *D) {}
107
108 void dispatchDeclAsContext(Decl *D) {
109 DISPATCH(visitDeclAsContext, Decl);
110 }
111 void visitDeclAsContext(Decl *D) {}
112
113#undef DISPATCH
114};
115
116template <class Impl> struct XMLTypeVisitor {
117#define DISPATCH(NAME, CLASS) \
118 static_cast<Impl*>(this)->NAME(static_cast<CLASS*>(T))
119
120 void dispatch(Type *T) {
121 switch (T->getTypeClass()) {
John McCall26c25c92010-11-24 11:30:07 +0000122#define TYPE(DERIVED, BASE) \
123 case Type::DERIVED: \
124 DISPATCH(dispatch##DERIVED##TypeAttrs, DERIVED##Type); \
125 static_cast<Impl*>(this)->completeAttrs(); \
126 DISPATCH(dispatch##DERIVED##TypeChildren, DERIVED##Type); \
127 break;
128#define ABSTRACT_TYPE(DERIVED, BASE)
129#include "clang/AST/TypeNodes.def"
130 }
131 }
132
133#define TYPE(DERIVED, BASE) \
134 void dispatch##DERIVED##TypeAttrs(DERIVED##Type *T) { \
135 DISPATCH(dispatch##BASE##Attrs, BASE); \
136 DISPATCH(visit##DERIVED##TypeAttrs, DERIVED##Type); \
137 } \
138 void visit##DERIVED##TypeAttrs(DERIVED##Type *T) {} \
139 void dispatch##DERIVED##TypeChildren(DERIVED##Type *T) { \
140 DISPATCH(dispatch##BASE##Children, BASE); \
141 DISPATCH(visit##DERIVED##TypeChildren, DERIVED##Type); \
142 } \
143 void visit##DERIVED##TypeChildren(DERIVED##Type *T) {}
144#include "clang/AST/TypeNodes.def"
145
146 void dispatchTypeAttrs(Type *T) {
147 DISPATCH(visitTypeAttrs, Type);
148 }
149 void visitTypeAttrs(Type *T) {}
150
151 void dispatchTypeChildren(Type *T) {
152 DISPATCH(visitTypeChildren, Type);
153 }
154 void visitTypeChildren(Type *T) {}
155
156#undef DISPATCH
157};
158
Chris Lattner5f9e2722011-07-23 10:55:15 +0000159static StringRef getTypeKindName(Type *T) {
John McCall26c25c92010-11-24 11:30:07 +0000160 switch (T->getTypeClass()) {
161#define TYPE(DERIVED, BASE) case Type::DERIVED: return #DERIVED "Type";
162#define ABSTRACT_TYPE(DERIVED, BASE)
163#include "clang/AST/TypeNodes.def"
164 }
165
166 llvm_unreachable("unknown type kind!");
John McCall26c25c92010-11-24 11:30:07 +0000167}
168
169struct XMLDumper : public XMLDeclVisitor<XMLDumper>,
170 public XMLTypeVisitor<XMLDumper> {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000171 raw_ostream &out;
John McCall26c25c92010-11-24 11:30:07 +0000172 ASTContext &Context;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000173 SmallVector<Node, 16> Stack;
John McCall26c25c92010-11-24 11:30:07 +0000174 unsigned Indent;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000175 explicit XMLDumper(raw_ostream &OS, ASTContext &context)
John McCall26c25c92010-11-24 11:30:07 +0000176 : out(OS), Context(context), Indent(0) {}
177
178 void indent() {
179 for (unsigned I = Indent; I; --I)
180 out << ' ';
181 }
182
183 /// Push a new node on the stack.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000184 void push(StringRef name) {
John McCall26c25c92010-11-24 11:30:07 +0000185 if (!Stack.empty()) {
186 assert(Stack.back().isDoneWithAttrs());
187 if (Stack.back().State == NS_LazyChildren) {
188 Stack.back().State = NS_Children;
189 out << ">\n";
190 }
191 Indent++;
192 indent();
193 }
194 Stack.push_back(Node(name));
195 out << '<' << name;
196 }
197
198 /// Set the given attribute to the given value.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000199 void set(StringRef attr, StringRef value) {
John McCall26c25c92010-11-24 11:30:07 +0000200 assert(!Stack.empty() && !Stack.back().isDoneWithAttrs());
201 out << ' ' << attr << '=' << '"' << value << '"'; // TODO: quotation
202 }
203
204 /// Finish attributes.
205 void completeAttrs() {
206 assert(!Stack.empty() && !Stack.back().isDoneWithAttrs());
207 Stack.back().State = NS_LazyChildren;
208 }
209
210 /// Pop a node.
211 void pop() {
212 assert(!Stack.empty() && Stack.back().isDoneWithAttrs());
213 if (Stack.back().State == NS_LazyChildren) {
214 out << "/>\n";
215 } else {
216 indent();
217 out << "</" << Stack.back().Name << ">\n";
218 }
219 if (Stack.size() > 1) Indent--;
220 Stack.pop_back();
221 }
222
223 //---- General utilities -------------------------------------------//
224
Chris Lattner5f9e2722011-07-23 10:55:15 +0000225 void setPointer(StringRef prop, const void *p) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000226 SmallString<10> buffer;
John McCall26c25c92010-11-24 11:30:07 +0000227 llvm::raw_svector_ostream os(buffer);
228 os << p;
229 os.flush();
230 set(prop, buffer);
231 }
232
233 void setPointer(void *p) {
234 setPointer("ptr", p);
235 }
236
Chris Lattner5f9e2722011-07-23 10:55:15 +0000237 void setInteger(StringRef prop, const llvm::APSInt &v) {
John McCall26c25c92010-11-24 11:30:07 +0000238 set(prop, v.toString(10));
239 }
240
Chris Lattner5f9e2722011-07-23 10:55:15 +0000241 void setInteger(StringRef prop, unsigned n) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000242 SmallString<10> buffer;
John McCall26c25c92010-11-24 11:30:07 +0000243 llvm::raw_svector_ostream os(buffer);
244 os << n;
245 os.flush();
246 set(prop, buffer);
247 }
248
Chris Lattner5f9e2722011-07-23 10:55:15 +0000249 void setFlag(StringRef prop, bool flag) {
John McCall26c25c92010-11-24 11:30:07 +0000250 if (flag) set(prop, "true");
251 }
252
253 void setName(DeclarationName Name) {
John McCall6710cf12010-11-30 10:12:16 +0000254 if (!Name)
255 return set("name", "");
256
John McCall26c25c92010-11-24 11:30:07 +0000257 // Common case.
258 if (Name.isIdentifier())
259 return set("name", Name.getAsIdentifierInfo()->getName());
260
John McCall7bd245b2010-12-02 10:37:08 +0000261 set("name", Name.getAsString());
John McCall26c25c92010-11-24 11:30:07 +0000262 }
263
264 class TemporaryContainer {
265 XMLDumper &Dumper;
266 public:
Chris Lattner5f9e2722011-07-23 10:55:15 +0000267 TemporaryContainer(XMLDumper &dumper, StringRef name)
John McCall26c25c92010-11-24 11:30:07 +0000268 : Dumper(dumper) {
269 Dumper.push(name);
270 Dumper.completeAttrs();
271 }
272
273 ~TemporaryContainer() {
274 Dumper.pop();
275 }
276 };
277
278 void visitTemplateParameters(TemplateParameterList *L) {
279 push("template_parameters");
280 completeAttrs();
281 for (TemplateParameterList::iterator
282 I = L->begin(), E = L->end(); I != E; ++I)
283 dispatch(*I);
284 pop();
285 }
286
287 void visitTemplateArguments(const TemplateArgumentList &L) {
288 push("template_arguments");
289 completeAttrs();
290 for (unsigned I = 0, E = L.size(); I != E; ++I)
291 dispatch(L[I]);
292 pop();
293 }
294
295 /// Visits a reference to the given declaration.
296 void visitDeclRef(Decl *D) {
297 push(D->getDeclKindName());
298 setPointer("ref", D);
299 completeAttrs();
300 pop();
301 }
Chris Lattner5f9e2722011-07-23 10:55:15 +0000302 void visitDeclRef(StringRef Name, Decl *D) {
John McCall3bddf5c2010-12-02 10:24:56 +0000303 TemporaryContainer C(*this, Name);
304 if (D) visitDeclRef(D);
305 }
John McCall26c25c92010-11-24 11:30:07 +0000306
307 void dispatch(const TemplateArgument &A) {
308 switch (A.getKind()) {
309 case TemplateArgument::Null: {
310 TemporaryContainer C(*this, "null");
311 break;
312 }
313 case TemplateArgument::Type: {
314 dispatch(A.getAsType());
315 break;
316 }
317 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000318 case TemplateArgument::TemplateExpansion:
319 // FIXME: Implement!
320 break;
321
John McCall26c25c92010-11-24 11:30:07 +0000322 case TemplateArgument::Declaration: {
323 visitDeclRef(A.getAsDecl());
324 break;
325 }
326 case TemplateArgument::Integral: {
327 push("integer");
328 setInteger("value", *A.getAsIntegral());
329 completeAttrs();
330 pop();
331 break;
332 }
333 case TemplateArgument::Expression: {
334 dispatch(A.getAsExpr());
335 break;
336 }
337 case TemplateArgument::Pack: {
Douglas Gregor87dd6972010-12-20 16:52:59 +0000338 for (TemplateArgument::pack_iterator P = A.pack_begin(),
339 PEnd = A.pack_end();
340 P != PEnd; ++P)
341 dispatch(*P);
John McCall26c25c92010-11-24 11:30:07 +0000342 break;
343 }
344 }
345 }
346
347 void dispatch(const TemplateArgumentLoc &A) {
348 dispatch(A.getArgument());
349 }
350
351 //---- Declarations ------------------------------------------------//
352 // Calls are made in this order:
353 // # Enter a new node.
354 // push("FieldDecl")
355 //
356 // # In this phase, attributes are set on the node.
357 // visitDeclAttrs(D)
358 // visitNamedDeclAttrs(D)
359 // ...
360 // visitFieldDeclAttrs(D)
361 //
362 // # No more attributes after this point.
363 // completeAttrs()
364 //
365 // # Create "header" child nodes, i.e. those which logically
366 // # belong to the declaration itself.
367 // visitDeclChildren(D)
368 // visitNamedDeclChildren(D)
369 // ...
370 // visitFieldDeclChildren(D)
371 //
372 // # Create nodes for the lexical children.
373 // visitDeclAsContext(D)
374 // visitNamedDeclAsContext(D)
375 // ...
376 // visitFieldDeclAsContext(D)
377 //
378 // # Finish the node.
379 // pop();
380 void dispatch(Decl *D) {
381 push(D->getDeclKindName());
John McCall13cf5e22010-11-24 11:53:13 +0000382 XMLDeclVisitor<XMLDumper>::dispatch(D);
John McCall26c25c92010-11-24 11:30:07 +0000383 pop();
384 }
385 void visitDeclAttrs(Decl *D) {
386 setPointer(D);
387 }
388
389 /// Visit all the lexical decls in the given context.
390 void visitDeclContext(DeclContext *DC) {
391 for (DeclContext::decl_iterator
392 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
393 dispatch(*I);
394
395 // FIXME: point out visible declarations not in lexical context?
396 }
397
398 /// Set the "access" attribute on the current node according to the
399 /// given specifier.
400 void setAccess(AccessSpecifier AS) {
401 switch (AS) {
402 case AS_public: return set("access", "public");
403 case AS_protected: return set("access", "protected");
404 case AS_private: return set("access", "private");
405 case AS_none: llvm_unreachable("explicit forbidden access");
406 }
407 }
408
409 template <class T> void visitRedeclarableAttrs(T *D) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000410 if (T *Prev = D->getPreviousDecl())
John McCall26c25c92010-11-24 11:30:07 +0000411 setPointer("previous", Prev);
412 }
413
414
415 // TranslationUnitDecl
416 void visitTranslationUnitDeclAsContext(TranslationUnitDecl *D) {
417 visitDeclContext(D);
418 }
419
420 // LinkageSpecDecl
421 void visitLinkageSpecDeclAttrs(LinkageSpecDecl *D) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000422 StringRef lang = "";
John McCall26c25c92010-11-24 11:30:07 +0000423 switch (D->getLanguage()) {
424 case LinkageSpecDecl::lang_c: lang = "C"; break;
425 case LinkageSpecDecl::lang_cxx: lang = "C++"; break;
426 }
427 set("lang", lang);
428 }
429 void visitLinkageSpecDeclAsContext(LinkageSpecDecl *D) {
430 visitDeclContext(D);
431 }
432
433 // NamespaceDecl
434 void visitNamespaceDeclAttrs(NamespaceDecl *D) {
435 setFlag("inline", D->isInline());
436 if (!D->isOriginalNamespace())
437 setPointer("original", D->getOriginalNamespace());
438 }
439 void visitNamespaceDeclAsContext(NamespaceDecl *D) {
440 visitDeclContext(D);
441 }
442
443 // NamedDecl
444 void visitNamedDeclAttrs(NamedDecl *D) {
445 setName(D->getDeclName());
446 }
447
448 // ValueDecl
449 void visitValueDeclChildren(ValueDecl *D) {
450 dispatch(D->getType());
451 }
452
453 // DeclaratorDecl
454 void visitDeclaratorDeclChildren(DeclaratorDecl *D) {
455 //dispatch(D->getTypeSourceInfo()->getTypeLoc());
456 }
457
458 // VarDecl
459 void visitVarDeclAttrs(VarDecl *D) {
460 visitRedeclarableAttrs(D);
461 if (D->getStorageClass() != SC_None)
462 set("storage",
463 VarDecl::getStorageClassSpecifierString(D->getStorageClass()));
464 setFlag("directinit", D->hasCXXDirectInitializer());
465 setFlag("nrvo", D->isNRVOVariable());
466 // TODO: instantiation, etc.
467 }
468 void visitVarDeclChildren(VarDecl *D) {
469 if (D->hasInit()) dispatch(D->getInit());
470 }
471
472 // ParmVarDecl?
473
474 // FunctionDecl
475 void visitFunctionDeclAttrs(FunctionDecl *D) {
476 visitRedeclarableAttrs(D);
477 setFlag("pure", D->isPure());
478 setFlag("trivial", D->isTrivial());
479 setFlag("returnzero", D->hasImplicitReturnZero());
480 setFlag("prototype", D->hasWrittenPrototype());
Sean Hunt10620eb2011-05-06 20:44:56 +0000481 setFlag("deleted", D->isDeletedAsWritten());
John McCall26c25c92010-11-24 11:30:07 +0000482 if (D->getStorageClass() != SC_None)
483 set("storage",
484 VarDecl::getStorageClassSpecifierString(D->getStorageClass()));
485 setFlag("inline", D->isInlineSpecified());
Joerg Sonnenbergercbea7632011-05-13 21:10:39 +0000486 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>())
487 set("asmlabel", ALA->getLabel());
John McCall26c25c92010-11-24 11:30:07 +0000488 // TODO: instantiation, etc.
489 }
490 void visitFunctionDeclChildren(FunctionDecl *D) {
491 for (FunctionDecl::param_iterator
492 I = D->param_begin(), E = D->param_end(); I != E; ++I)
493 dispatch(*I);
Sean Hunt10620eb2011-05-06 20:44:56 +0000494 if (D->doesThisDeclarationHaveABody())
John McCall26c25c92010-11-24 11:30:07 +0000495 dispatch(D->getBody());
496 }
497
498 // CXXMethodDecl ?
499 // CXXConstructorDecl ?
500 // CXXDestructorDecl ?
501 // CXXConversionDecl ?
502
Sean Huntcbb67482011-01-08 20:30:50 +0000503 void dispatch(CXXCtorInitializer *Init) {
John McCall3bddf5c2010-12-02 10:24:56 +0000504 // TODO
505 }
506
John McCall26c25c92010-11-24 11:30:07 +0000507 // FieldDecl
508 void visitFieldDeclAttrs(FieldDecl *D) {
509 setFlag("mutable", D->isMutable());
510 }
511 void visitFieldDeclChildren(FieldDecl *D) {
512 if (D->isBitField()) {
513 TemporaryContainer C(*this, "bitwidth");
514 dispatch(D->getBitWidth());
515 }
516 // TODO: C++0x member initializer
517 }
518
519 // EnumConstantDecl
520 void visitEnumConstantDeclChildren(EnumConstantDecl *D) {
521 // value in any case?
522 if (D->getInitExpr()) dispatch(D->getInitExpr());
523 }
524
525 // IndirectFieldDecl
526 void visitIndirectFieldDeclChildren(IndirectFieldDecl *D) {
527 for (IndirectFieldDecl::chain_iterator
528 I = D->chain_begin(), E = D->chain_end(); I != E; ++I) {
529 NamedDecl *VD = const_cast<NamedDecl*>(*I);
530 push(isa<VarDecl>(VD) ? "variable" : "field");
531 setPointer("ptr", VD);
532 completeAttrs();
533 pop();
534 }
535 }
536
537 // TypeDecl
538 void visitTypeDeclAttrs(TypeDecl *D) {
539 setPointer("typeptr", D->getTypeForDecl());
540 }
541
542 // TypedefDecl
543 void visitTypedefDeclAttrs(TypedefDecl *D) {
Richard Smith162e1c12011-04-15 14:24:37 +0000544 visitRedeclarableAttrs<TypedefNameDecl>(D);
John McCall26c25c92010-11-24 11:30:07 +0000545 }
546 void visitTypedefDeclChildren(TypedefDecl *D) {
547 dispatch(D->getTypeSourceInfo()->getTypeLoc());
548 }
549
Richard Smith162e1c12011-04-15 14:24:37 +0000550 // TypeAliasDecl
551 void visitTypeAliasDeclAttrs(TypeAliasDecl *D) {
552 visitRedeclarableAttrs<TypedefNameDecl>(D);
553 }
554 void visitTypeAliasDeclChildren(TypeAliasDecl *D) {
555 dispatch(D->getTypeSourceInfo()->getTypeLoc());
556 }
557
John McCall26c25c92010-11-24 11:30:07 +0000558 // TagDecl
559 void visitTagDeclAttrs(TagDecl *D) {
560 visitRedeclarableAttrs(D);
561 }
562 void visitTagDeclAsContext(TagDecl *D) {
563 visitDeclContext(D);
564 }
565
566 // EnumDecl
567 void visitEnumDeclAttrs(EnumDecl *D) {
568 setFlag("scoped", D->isScoped());
569 setFlag("fixed", D->isFixed());
570 }
571 void visitEnumDeclChildren(EnumDecl *D) {
572 {
573 TemporaryContainer C(*this, "promotion_type");
574 dispatch(D->getPromotionType());
575 }
576 {
577 TemporaryContainer C(*this, "integer_type");
578 dispatch(D->getIntegerType());
579 }
580 }
581
582 // RecordDecl ?
583
584 void visitCXXRecordDeclChildren(CXXRecordDecl *D) {
585 if (!D->isThisDeclarationADefinition()) return;
586
587 for (CXXRecordDecl::base_class_iterator
588 I = D->bases_begin(), E = D->bases_end(); I != E; ++I) {
589 push("base");
590 setAccess(I->getAccessSpecifier());
591 completeAttrs();
592 dispatch(I->getTypeSourceInfo()->getTypeLoc());
593 pop();
594 }
595 }
596
597 // ClassTemplateSpecializationDecl ?
598
599 // FileScopeAsmDecl ?
600
601 // BlockDecl
602 void visitBlockDeclAttrs(BlockDecl *D) {
603 setFlag("variadic", D->isVariadic());
604 }
605 void visitBlockDeclChildren(BlockDecl *D) {
606 for (FunctionDecl::param_iterator
607 I = D->param_begin(), E = D->param_end(); I != E; ++I)
608 dispatch(*I);
609 dispatch(D->getBody());
610 }
611
612 // AccessSpecDecl
613 void visitAccessSpecDeclAttrs(AccessSpecDecl *D) {
614 setAccess(D->getAccess());
615 }
616
617 // TemplateDecl
618 void visitTemplateDeclChildren(TemplateDecl *D) {
619 visitTemplateParameters(D->getTemplateParameters());
Richard Smith3e4c6c42011-05-05 21:57:07 +0000620 if (D->getTemplatedDecl())
621 dispatch(D->getTemplatedDecl());
John McCall26c25c92010-11-24 11:30:07 +0000622 }
623
624 // FunctionTemplateDecl
625 void visitFunctionTemplateDeclAttrs(FunctionTemplateDecl *D) {
626 visitRedeclarableAttrs(D);
627 }
628 void visitFunctionTemplateDeclChildren(FunctionTemplateDecl *D) {
629 // Mention all the specializations which don't have explicit
630 // declarations elsewhere.
631 for (FunctionTemplateDecl::spec_iterator
632 I = D->spec_begin(), E = D->spec_end(); I != E; ++I) {
633 FunctionTemplateSpecializationInfo *Info
634 = I->getTemplateSpecializationInfo();
635
636 bool Unknown = false;
637 switch (Info->getTemplateSpecializationKind()) {
638 case TSK_ImplicitInstantiation: Unknown = false; break;
639 case TSK_Undeclared: Unknown = true; break;
640
641 // These will be covered at their respective sites.
642 case TSK_ExplicitSpecialization: continue;
643 case TSK_ExplicitInstantiationDeclaration: continue;
644 case TSK_ExplicitInstantiationDefinition: continue;
645 }
646
647 TemporaryContainer C(*this,
648 Unknown ? "uninstantiated" : "instantiation");
649 visitTemplateArguments(*Info->TemplateArguments);
650 dispatch(Info->Function);
651 }
652 }
653
654 // ClasTemplateDecl
655 void visitClassTemplateDeclAttrs(ClassTemplateDecl *D) {
656 visitRedeclarableAttrs(D);
657 }
658 void visitClassTemplateDeclChildren(ClassTemplateDecl *D) {
659 // Mention all the specializations which don't have explicit
660 // declarations elsewhere.
661 for (ClassTemplateDecl::spec_iterator
662 I = D->spec_begin(), E = D->spec_end(); I != E; ++I) {
663
664 bool Unknown = false;
665 switch (I->getTemplateSpecializationKind()) {
666 case TSK_ImplicitInstantiation: Unknown = false; break;
667 case TSK_Undeclared: Unknown = true; break;
668
669 // These will be covered at their respective sites.
670 case TSK_ExplicitSpecialization: continue;
671 case TSK_ExplicitInstantiationDeclaration: continue;
672 case TSK_ExplicitInstantiationDefinition: continue;
673 }
674
675 TemporaryContainer C(*this,
676 Unknown ? "uninstantiated" : "instantiation");
677 visitTemplateArguments(I->getTemplateArgs());
678 dispatch(*I);
679 }
680 }
681
682 // TemplateTypeParmDecl
683 void visitTemplateTypeParmDeclAttrs(TemplateTypeParmDecl *D) {
684 setInteger("depth", D->getDepth());
685 setInteger("index", D->getIndex());
686 }
687 void visitTemplateTypeParmDeclChildren(TemplateTypeParmDecl *D) {
688 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
689 dispatch(D->getDefaultArgumentInfo()->getTypeLoc());
690 // parameter pack?
691 }
692
693 // NonTypeTemplateParmDecl
694 void visitNonTypeTemplateParmDeclAttrs(NonTypeTemplateParmDecl *D) {
695 setInteger("depth", D->getDepth());
696 setInteger("index", D->getIndex());
697 }
698 void visitNonTypeTemplateParmDeclChildren(NonTypeTemplateParmDecl *D) {
699 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
700 dispatch(D->getDefaultArgument());
701 // parameter pack?
702 }
703
704 // TemplateTemplateParmDecl
705 void visitTemplateTemplateParmDeclAttrs(TemplateTemplateParmDecl *D) {
706 setInteger("depth", D->getDepth());
707 setInteger("index", D->getIndex());
708 }
709 void visitTemplateTemplateParmDeclChildren(TemplateTemplateParmDecl *D) {
710 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
711 dispatch(D->getDefaultArgument());
712 // parameter pack?
713 }
714
715 // FriendDecl
716 void visitFriendDeclChildren(FriendDecl *D) {
717 if (TypeSourceInfo *T = D->getFriendType())
718 dispatch(T->getTypeLoc());
719 else
720 dispatch(D->getFriendDecl());
721 }
722
723 // UsingDirectiveDecl ?
724 // UsingDecl ?
725 // UsingShadowDecl ?
726 // NamespaceAliasDecl ?
727 // UnresolvedUsingValueDecl ?
728 // UnresolvedUsingTypenameDecl ?
729 // StaticAssertDecl ?
730
John McCall3bddf5c2010-12-02 10:24:56 +0000731 // ObjCImplDecl
732 void visitObjCImplDeclChildren(ObjCImplDecl *D) {
733 visitDeclRef(D->getClassInterface());
734 }
735 void visitObjCImplDeclAsContext(ObjCImplDecl *D) {
736 visitDeclContext(D);
737 }
738
John McCall3bddf5c2010-12-02 10:24:56 +0000739 // ObjCInterfaceDecl
740 void visitCategoryList(ObjCCategoryDecl *D) {
741 if (!D) return;
742
743 TemporaryContainer C(*this, "categories");
744 for (; D; D = D->getNextClassCategory())
745 visitDeclRef(D);
746 }
747 void visitObjCInterfaceDeclAttrs(ObjCInterfaceDecl *D) {
748 setPointer("typeptr", D->getTypeForDecl());
Douglas Gregor7723fec2011-12-15 20:29:51 +0000749 setFlag("forward_decl", !D->isThisDeclarationADefinition());
John McCall3bddf5c2010-12-02 10:24:56 +0000750 setFlag("implicit_interface", D->isImplicitInterfaceDecl());
751 }
752 void visitObjCInterfaceDeclChildren(ObjCInterfaceDecl *D) {
753 visitDeclRef("super", D->getSuperClass());
754 visitDeclRef("implementation", D->getImplementation());
755 if (D->protocol_begin() != D->protocol_end()) {
756 TemporaryContainer C(*this, "protocols");
757 for (ObjCInterfaceDecl::protocol_iterator
758 I = D->protocol_begin(), E = D->protocol_end(); I != E; ++I)
759 visitDeclRef(*I);
760 }
761 visitCategoryList(D->getCategoryList());
762 }
763 void visitObjCInterfaceDeclAsContext(ObjCInterfaceDecl *D) {
764 visitDeclContext(D);
765 }
766
767 // ObjCCategoryDecl
768 void visitObjCCategoryDeclAttrs(ObjCCategoryDecl *D) {
769 setFlag("extension", D->IsClassExtension());
770 setFlag("synth_bitfield", D->hasSynthBitfield());
771 }
772 void visitObjCCategoryDeclChildren(ObjCCategoryDecl *D) {
773 visitDeclRef("interface", D->getClassInterface());
774 visitDeclRef("implementation", D->getImplementation());
775 if (D->protocol_begin() != D->protocol_end()) {
776 TemporaryContainer C(*this, "protocols");
777 for (ObjCCategoryDecl::protocol_iterator
778 I = D->protocol_begin(), E = D->protocol_end(); I != E; ++I)
779 visitDeclRef(*I);
780 }
781 }
782 void visitObjCCategoryDeclAsContext(ObjCCategoryDecl *D) {
783 visitDeclContext(D);
784 }
785
786 // ObjCCategoryImplDecl
787 void visitObjCCategoryImplDeclAttrs(ObjCCategoryImplDecl *D) {
788 set("identifier", D->getName());
789 }
790 void visitObjCCategoryImplDeclChildren(ObjCCategoryImplDecl *D) {
791 visitDeclRef(D->getCategoryDecl());
792 }
793
794 // ObjCImplementationDecl
795 void visitObjCImplementationDeclAttrs(ObjCImplementationDecl *D) {
796 setFlag("synth_bitfield", D->hasSynthBitfield());
797 set("identifier", D->getName());
798 }
799 void visitObjCImplementationDeclChildren(ObjCImplementationDecl *D) {
800 visitDeclRef("super", D->getSuperClass());
801 if (D->init_begin() != D->init_end()) {
802 TemporaryContainer C(*this, "initializers");
803 for (ObjCImplementationDecl::init_iterator
804 I = D->init_begin(), E = D->init_end(); I != E; ++I)
805 dispatch(*I);
806 }
807 }
808
John McCall3bddf5c2010-12-02 10:24:56 +0000809 // ObjCProtocolDecl
John McCall3bddf5c2010-12-02 10:24:56 +0000810 void visitObjCProtocolDeclChildren(ObjCProtocolDecl *D) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000811 if (!D->isThisDeclarationADefinition())
812 return;
813
John McCall3bddf5c2010-12-02 10:24:56 +0000814 if (D->protocol_begin() != D->protocol_end()) {
815 TemporaryContainer C(*this, "protocols");
816 for (ObjCInterfaceDecl::protocol_iterator
817 I = D->protocol_begin(), E = D->protocol_end(); I != E; ++I)
818 visitDeclRef(*I);
819 }
820 }
821 void visitObjCProtocolDeclAsContext(ObjCProtocolDecl *D) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000822 if (!D->isThisDeclarationADefinition())
823 return;
824
John McCall3bddf5c2010-12-02 10:24:56 +0000825 visitDeclContext(D);
826 }
827
828 // ObjCMethodDecl
829 void visitObjCMethodDeclAttrs(ObjCMethodDecl *D) {
830 // decl qualifier?
831 // implementation control?
832
833 setFlag("instance", D->isInstanceMethod());
834 setFlag("variadic", D->isVariadic());
835 setFlag("synthesized", D->isSynthesized());
836 setFlag("defined", D->isDefined());
Douglas Gregor926df6c2011-06-11 01:09:30 +0000837 setFlag("related_result_type", D->hasRelatedResultType());
John McCall3bddf5c2010-12-02 10:24:56 +0000838 }
839 void visitObjCMethodDeclChildren(ObjCMethodDecl *D) {
840 dispatch(D->getResultType());
841 for (ObjCMethodDecl::param_iterator
842 I = D->param_begin(), E = D->param_end(); I != E; ++I)
843 dispatch(*I);
844 if (D->isThisDeclarationADefinition())
845 dispatch(D->getBody());
846 }
847
848 // ObjCIvarDecl
Chris Lattner5f9e2722011-07-23 10:55:15 +0000849 void setAccessControl(StringRef prop, ObjCIvarDecl::AccessControl AC) {
John McCall3bddf5c2010-12-02 10:24:56 +0000850 switch (AC) {
851 case ObjCIvarDecl::None: return set(prop, "none");
852 case ObjCIvarDecl::Private: return set(prop, "private");
853 case ObjCIvarDecl::Protected: return set(prop, "protected");
854 case ObjCIvarDecl::Public: return set(prop, "public");
855 case ObjCIvarDecl::Package: return set(prop, "package");
856 }
857 }
858 void visitObjCIvarDeclAttrs(ObjCIvarDecl *D) {
859 setFlag("synthesize", D->getSynthesize());
860 setAccessControl("access", D->getAccessControl());
861 }
862
863 // ObjCCompatibleAliasDecl
864 void visitObjCCompatibleAliasDeclChildren(ObjCCompatibleAliasDecl *D) {
865 visitDeclRef(D->getClassInterface());
866 }
867
868 // FIXME: ObjCPropertyDecl
869 // FIXME: ObjCPropertyImplDecl
870
John McCall26c25c92010-11-24 11:30:07 +0000871 //---- Types -----------------------------------------------------//
872 void dispatch(TypeLoc TL) {
873 dispatch(TL.getType()); // for now
874 }
875
876 void dispatch(QualType T) {
877 if (T.hasLocalQualifiers()) {
878 push("QualType");
879 Qualifiers Qs = T.getLocalQualifiers();
880 setFlag("const", Qs.hasConst());
881 setFlag("volatile", Qs.hasVolatile());
882 setFlag("restrict", Qs.hasRestrict());
883 if (Qs.hasAddressSpace()) setInteger("addrspace", Qs.getAddressSpace());
884 if (Qs.hasObjCGCAttr()) {
885 switch (Qs.getObjCGCAttr()) {
886 case Qualifiers::Weak: set("gc", "weak"); break;
887 case Qualifiers::Strong: set("gc", "strong"); break;
888 case Qualifiers::GCNone: llvm_unreachable("explicit none");
889 }
890 }
891
892 completeAttrs();
893 dispatch(QualType(T.getTypePtr(), 0));
894 pop();
895 return;
896 }
897
898 Type *Ty = const_cast<Type*>(T.getTypePtr());
899 push(getTypeKindName(Ty));
John McCall13cf5e22010-11-24 11:53:13 +0000900 XMLTypeVisitor<XMLDumper>::dispatch(const_cast<Type*>(T.getTypePtr()));
John McCall26c25c92010-11-24 11:30:07 +0000901 pop();
902 }
903
904 void setCallingConv(CallingConv CC) {
905 switch (CC) {
906 case CC_Default: return;
907 case CC_C: return set("cc", "cdecl");
908 case CC_X86FastCall: return set("cc", "x86_fastcall");
909 case CC_X86StdCall: return set("cc", "x86_stdcall");
910 case CC_X86ThisCall: return set("cc", "x86_thiscall");
911 case CC_X86Pascal: return set("cc", "x86_pascal");
Anton Korobeynikov414d8962011-04-14 20:06:49 +0000912 case CC_AAPCS: return set("cc", "aapcs");
913 case CC_AAPCS_VFP: return set("cc", "aapcs_vfp");
John McCall26c25c92010-11-24 11:30:07 +0000914 }
915 }
916
917 void visitTypeAttrs(Type *D) {
918 setPointer(D);
919 setFlag("dependent", D->isDependentType());
920 setFlag("variably_modified", D->isVariablyModifiedType());
921
922 setPointer("canonical", D->getCanonicalTypeInternal().getAsOpaquePtr());
923 }
924
925 void visitPointerTypeChildren(PointerType *T) {
926 dispatch(T->getPointeeType());
927 }
928 void visitReferenceTypeChildren(ReferenceType *T) {
929 dispatch(T->getPointeeType());
930 }
931 void visitObjCObjectPointerTypeChildren(ObjCObjectPointerType *T) {
932 dispatch(T->getPointeeType());
933 }
934 void visitBlockPointerTypeChildren(BlockPointerType *T) {
935 dispatch(T->getPointeeType());
936 }
937
938 // Types that just wrap declarations.
939 void visitTagTypeChildren(TagType *T) {
940 visitDeclRef(T->getDecl());
941 }
942 void visitTypedefTypeChildren(TypedefType *T) {
943 visitDeclRef(T->getDecl());
944 }
945 void visitObjCInterfaceTypeChildren(ObjCInterfaceType *T) {
946 visitDeclRef(T->getDecl());
947 }
948 void visitUnresolvedUsingTypeChildren(UnresolvedUsingType *T) {
949 visitDeclRef(T->getDecl());
950 }
951 void visitInjectedClassNameTypeChildren(InjectedClassNameType *T) {
952 visitDeclRef(T->getDecl());
953 }
954
955 void visitFunctionTypeAttrs(FunctionType *T) {
956 setFlag("noreturn", T->getNoReturnAttr());
957 setCallingConv(T->getCallConv());
Eli Friedmana49218e2011-04-09 08:18:08 +0000958 if (T->getHasRegParm()) setInteger("regparm", T->getRegParmType());
John McCall26c25c92010-11-24 11:30:07 +0000959 }
960 void visitFunctionTypeChildren(FunctionType *T) {
961 dispatch(T->getResultType());
962 }
963
964 void visitFunctionProtoTypeAttrs(FunctionProtoType *T) {
965 setFlag("const", T->getTypeQuals() & Qualifiers::Const);
966 setFlag("volatile", T->getTypeQuals() & Qualifiers::Volatile);
967 setFlag("restrict", T->getTypeQuals() & Qualifiers::Restrict);
968 }
969 void visitFunctionProtoTypeChildren(FunctionProtoType *T) {
970 push("parameters");
971 setFlag("variadic", T->isVariadic());
972 completeAttrs();
973 for (FunctionProtoType::arg_type_iterator
974 I = T->arg_type_begin(), E = T->arg_type_end(); I != E; ++I)
975 dispatch(*I);
976 pop();
977
Sebastian Redl60618fa2011-03-12 11:50:43 +0000978 if (T->hasDynamicExceptionSpec()) {
John McCall26c25c92010-11-24 11:30:07 +0000979 push("exception_specifiers");
Sebastian Redl60618fa2011-03-12 11:50:43 +0000980 setFlag("any", T->getExceptionSpecType() == EST_MSAny);
John McCall26c25c92010-11-24 11:30:07 +0000981 completeAttrs();
982 for (FunctionProtoType::exception_iterator
983 I = T->exception_begin(), E = T->exception_end(); I != E; ++I)
984 dispatch(*I);
985 pop();
986 }
Sebastian Redl60618fa2011-03-12 11:50:43 +0000987 // FIXME: noexcept specifier
John McCall26c25c92010-11-24 11:30:07 +0000988 }
989
990 void visitTemplateSpecializationTypeChildren(TemplateSpecializationType *T) {
991 if (const RecordType *RT = T->getAs<RecordType>())
992 visitDeclRef(RT->getDecl());
993
994 // TODO: TemplateName
995
996 push("template_arguments");
997 completeAttrs();
998 for (unsigned I = 0, E = T->getNumArgs(); I != E; ++I)
999 dispatch(T->getArg(I));
1000 pop();
1001 }
1002
1003 //---- Statements ------------------------------------------------//
1004 void dispatch(Stmt *S) {
1005 // FIXME: this is not really XML at all
1006 push("Stmt");
John McCall3bddf5c2010-12-02 10:24:56 +00001007 out << ">\n";
John McCall26c25c92010-11-24 11:30:07 +00001008 Stack.back().State = NS_Children; // explicitly become non-lazy
1009 S->dump(out, Context.getSourceManager());
1010 out << '\n';
1011 pop();
1012 }
1013};
1014}
1015
1016void Decl::dumpXML() const {
1017 dumpXML(llvm::errs());
1018}
1019
Chris Lattner5f9e2722011-07-23 10:55:15 +00001020void Decl::dumpXML(raw_ostream &out) const {
John McCall26c25c92010-11-24 11:30:07 +00001021 XMLDumper(out, getASTContext()).dispatch(const_cast<Decl*>(this));
1022}
1023
1024#else /* ifndef NDEBUG */
1025
1026void Decl::dumpXML() const {}
Chris Lattner5f9e2722011-07-23 10:55:15 +00001027void Decl::dumpXML(raw_ostream &out) const {}
John McCall26c25c92010-11-24 11:30:07 +00001028
1029#endif