blob: 7465ea410d73e36161984701b30d2dad037f6fa1 [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"
42#include "llvm/ADT/SmallVector.h"
43#include "llvm/ADT/StringRef.h"
44
45using namespace clang;
46
47#ifndef NDEBUG
48
49namespace {
50
51enum NodeState {
52 NS_Attrs, NS_LazyChildren, NS_Children
53};
54
55struct Node {
56 llvm::StringRef Name;
57 NodeState State;
58 Node(llvm::StringRef name) : Name(name), State(NS_Attrs) {}
59
60 bool isDoneWithAttrs() const { return State != NS_Attrs; }
61};
62
63template <class Impl> struct XMLDeclVisitor {
64#define DISPATCH(NAME, CLASS) \
65 static_cast<Impl*>(this)->NAME(static_cast<CLASS*>(D))
66
67 void dispatch(Decl *D) {
68 switch (D->getKind()) {
69 default: llvm_unreachable("Decl that isn't part of DeclNodes.inc!");
70#define DECL(DERIVED, BASE) \
71 case Decl::DERIVED: \
72 DISPATCH(dispatch##DERIVED##DeclAttrs, DERIVED##Decl); \
73 static_cast<Impl*>(this)->completeAttrs(); \
74 DISPATCH(dispatch##DERIVED##DeclChildren, DERIVED##Decl); \
75 DISPATCH(dispatch##DERIVED##DeclAsContext, DERIVED##Decl); \
76 break;
77#define ABSTRACT_DECL(DECL)
78#include "clang/AST/DeclNodes.inc"
79 }
80 }
81
82#define DECL(DERIVED, BASE) \
83 void dispatch##DERIVED##DeclAttrs(DERIVED##Decl *D) { \
84 DISPATCH(dispatch##BASE##Attrs, BASE); \
85 DISPATCH(visit##DERIVED##DeclAttrs, DERIVED##Decl); \
86 } \
87 void visit##DERIVED##DeclAttrs(DERIVED##Decl *D) {} \
88 void dispatch##DERIVED##DeclChildren(DERIVED##Decl *D) { \
89 DISPATCH(dispatch##BASE##Children, BASE); \
90 DISPATCH(visit##DERIVED##DeclChildren, DERIVED##Decl); \
91 } \
92 void visit##DERIVED##DeclChildren(DERIVED##Decl *D) {} \
93 void dispatch##DERIVED##DeclAsContext(DERIVED##Decl *D) { \
94 DISPATCH(dispatch##BASE##AsContext, BASE); \
95 DISPATCH(visit##DERIVED##DeclAsContext, DERIVED##Decl); \
96 } \
97 void visit##DERIVED##DeclAsContext(DERIVED##Decl *D) {}
98#include "clang/AST/DeclNodes.inc"
99
100 void dispatchDeclAttrs(Decl *D) {
101 DISPATCH(visitDeclAttrs, Decl);
102 }
103 void visitDeclAttrs(Decl *D) {}
104
105 void dispatchDeclChildren(Decl *D) {
106 DISPATCH(visitDeclChildren, Decl);
107 }
108 void visitDeclChildren(Decl *D) {}
109
110 void dispatchDeclAsContext(Decl *D) {
111 DISPATCH(visitDeclAsContext, Decl);
112 }
113 void visitDeclAsContext(Decl *D) {}
114
115#undef DISPATCH
116};
117
118template <class Impl> struct XMLTypeVisitor {
119#define DISPATCH(NAME, CLASS) \
120 static_cast<Impl*>(this)->NAME(static_cast<CLASS*>(T))
121
122 void dispatch(Type *T) {
123 switch (T->getTypeClass()) {
124 default: llvm_unreachable("Type that isn't part of TypeNodes.inc!");
125#define TYPE(DERIVED, BASE) \
126 case Type::DERIVED: \
127 DISPATCH(dispatch##DERIVED##TypeAttrs, DERIVED##Type); \
128 static_cast<Impl*>(this)->completeAttrs(); \
129 DISPATCH(dispatch##DERIVED##TypeChildren, DERIVED##Type); \
130 break;
131#define ABSTRACT_TYPE(DERIVED, BASE)
132#include "clang/AST/TypeNodes.def"
133 }
134 }
135
136#define TYPE(DERIVED, BASE) \
137 void dispatch##DERIVED##TypeAttrs(DERIVED##Type *T) { \
138 DISPATCH(dispatch##BASE##Attrs, BASE); \
139 DISPATCH(visit##DERIVED##TypeAttrs, DERIVED##Type); \
140 } \
141 void visit##DERIVED##TypeAttrs(DERIVED##Type *T) {} \
142 void dispatch##DERIVED##TypeChildren(DERIVED##Type *T) { \
143 DISPATCH(dispatch##BASE##Children, BASE); \
144 DISPATCH(visit##DERIVED##TypeChildren, DERIVED##Type); \
145 } \
146 void visit##DERIVED##TypeChildren(DERIVED##Type *T) {}
147#include "clang/AST/TypeNodes.def"
148
149 void dispatchTypeAttrs(Type *T) {
150 DISPATCH(visitTypeAttrs, Type);
151 }
152 void visitTypeAttrs(Type *T) {}
153
154 void dispatchTypeChildren(Type *T) {
155 DISPATCH(visitTypeChildren, Type);
156 }
157 void visitTypeChildren(Type *T) {}
158
159#undef DISPATCH
160};
161
162static llvm::StringRef getTypeKindName(Type *T) {
163 switch (T->getTypeClass()) {
164#define TYPE(DERIVED, BASE) case Type::DERIVED: return #DERIVED "Type";
165#define ABSTRACT_TYPE(DERIVED, BASE)
166#include "clang/AST/TypeNodes.def"
167 }
168
169 llvm_unreachable("unknown type kind!");
170 return "unknown_type";
171}
172
173struct XMLDumper : public XMLDeclVisitor<XMLDumper>,
174 public XMLTypeVisitor<XMLDumper> {
175 llvm::raw_ostream &out;
176 ASTContext &Context;
177 llvm::SmallVector<Node, 16> Stack;
178 unsigned Indent;
179 explicit XMLDumper(llvm::raw_ostream &OS, ASTContext &context)
180 : out(OS), Context(context), Indent(0) {}
181
182 void indent() {
183 for (unsigned I = Indent; I; --I)
184 out << ' ';
185 }
186
187 /// Push a new node on the stack.
188 void push(llvm::StringRef name) {
189 if (!Stack.empty()) {
190 assert(Stack.back().isDoneWithAttrs());
191 if (Stack.back().State == NS_LazyChildren) {
192 Stack.back().State = NS_Children;
193 out << ">\n";
194 }
195 Indent++;
196 indent();
197 }
198 Stack.push_back(Node(name));
199 out << '<' << name;
200 }
201
202 /// Set the given attribute to the given value.
203 void set(llvm::StringRef attr, llvm::StringRef value) {
204 assert(!Stack.empty() && !Stack.back().isDoneWithAttrs());
205 out << ' ' << attr << '=' << '"' << value << '"'; // TODO: quotation
206 }
207
208 /// Finish attributes.
209 void completeAttrs() {
210 assert(!Stack.empty() && !Stack.back().isDoneWithAttrs());
211 Stack.back().State = NS_LazyChildren;
212 }
213
214 /// Pop a node.
215 void pop() {
216 assert(!Stack.empty() && Stack.back().isDoneWithAttrs());
217 if (Stack.back().State == NS_LazyChildren) {
218 out << "/>\n";
219 } else {
220 indent();
221 out << "</" << Stack.back().Name << ">\n";
222 }
223 if (Stack.size() > 1) Indent--;
224 Stack.pop_back();
225 }
226
227 //---- General utilities -------------------------------------------//
228
229 void setPointer(llvm::StringRef prop, void *p) {
230 llvm::SmallString<10> buffer;
231 llvm::raw_svector_ostream os(buffer);
232 os << p;
233 os.flush();
234 set(prop, buffer);
235 }
236
237 void setPointer(void *p) {
238 setPointer("ptr", p);
239 }
240
241 void setInteger(llvm::StringRef prop, const llvm::APSInt &v) {
242 set(prop, v.toString(10));
243 }
244
245 void setInteger(llvm::StringRef prop, unsigned n) {
246 llvm::SmallString<10> buffer;
247 llvm::raw_svector_ostream os(buffer);
248 os << n;
249 os.flush();
250 set(prop, buffer);
251 }
252
253 void setFlag(llvm::StringRef prop, bool flag) {
254 if (flag) set(prop, "true");
255 }
256
257 void setName(DeclarationName Name) {
John McCall6710cf12010-11-30 10:12:16 +0000258 if (!Name)
259 return set("name", "");
260
John McCall26c25c92010-11-24 11:30:07 +0000261 // Common case.
262 if (Name.isIdentifier())
263 return set("name", Name.getAsIdentifierInfo()->getName());
264
John McCall7bd245b2010-12-02 10:37:08 +0000265 set("name", Name.getAsString());
John McCall26c25c92010-11-24 11:30:07 +0000266 }
267
268 class TemporaryContainer {
269 XMLDumper &Dumper;
270 public:
271 TemporaryContainer(XMLDumper &dumper, llvm::StringRef name)
272 : Dumper(dumper) {
273 Dumper.push(name);
274 Dumper.completeAttrs();
275 }
276
277 ~TemporaryContainer() {
278 Dumper.pop();
279 }
280 };
281
282 void visitTemplateParameters(TemplateParameterList *L) {
283 push("template_parameters");
284 completeAttrs();
285 for (TemplateParameterList::iterator
286 I = L->begin(), E = L->end(); I != E; ++I)
287 dispatch(*I);
288 pop();
289 }
290
291 void visitTemplateArguments(const TemplateArgumentList &L) {
292 push("template_arguments");
293 completeAttrs();
294 for (unsigned I = 0, E = L.size(); I != E; ++I)
295 dispatch(L[I]);
296 pop();
297 }
298
299 /// Visits a reference to the given declaration.
300 void visitDeclRef(Decl *D) {
301 push(D->getDeclKindName());
302 setPointer("ref", D);
303 completeAttrs();
304 pop();
305 }
John McCall3bddf5c2010-12-02 10:24:56 +0000306 void visitDeclRef(llvm::StringRef Name, Decl *D) {
307 TemporaryContainer C(*this, Name);
308 if (D) visitDeclRef(D);
309 }
John McCall26c25c92010-11-24 11:30:07 +0000310
311 void dispatch(const TemplateArgument &A) {
312 switch (A.getKind()) {
313 case TemplateArgument::Null: {
314 TemporaryContainer C(*this, "null");
315 break;
316 }
317 case TemplateArgument::Type: {
318 dispatch(A.getAsType());
319 break;
320 }
321 case TemplateArgument::Template:
322 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) {
410 if (T *Prev = D->getPreviousDeclaration())
411 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) {
422 llvm::StringRef lang = "";
423 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());
481 setFlag("deleted", D->isDeleted());
482 if (D->getStorageClass() != SC_None)
483 set("storage",
484 VarDecl::getStorageClassSpecifierString(D->getStorageClass()));
485 setFlag("inline", D->isInlineSpecified());
486 // TODO: instantiation, etc.
487 }
488 void visitFunctionDeclChildren(FunctionDecl *D) {
489 for (FunctionDecl::param_iterator
490 I = D->param_begin(), E = D->param_end(); I != E; ++I)
491 dispatch(*I);
492 if (D->isThisDeclarationADefinition())
493 dispatch(D->getBody());
494 }
495
496 // CXXMethodDecl ?
497 // CXXConstructorDecl ?
498 // CXXDestructorDecl ?
499 // CXXConversionDecl ?
500
John McCall3bddf5c2010-12-02 10:24:56 +0000501 void dispatch(CXXBaseOrMemberInitializer *Init) {
502 // TODO
503 }
504
John McCall26c25c92010-11-24 11:30:07 +0000505 // FieldDecl
506 void visitFieldDeclAttrs(FieldDecl *D) {
507 setFlag("mutable", D->isMutable());
508 }
509 void visitFieldDeclChildren(FieldDecl *D) {
510 if (D->isBitField()) {
511 TemporaryContainer C(*this, "bitwidth");
512 dispatch(D->getBitWidth());
513 }
514 // TODO: C++0x member initializer
515 }
516
517 // EnumConstantDecl
518 void visitEnumConstantDeclChildren(EnumConstantDecl *D) {
519 // value in any case?
520 if (D->getInitExpr()) dispatch(D->getInitExpr());
521 }
522
523 // IndirectFieldDecl
524 void visitIndirectFieldDeclChildren(IndirectFieldDecl *D) {
525 for (IndirectFieldDecl::chain_iterator
526 I = D->chain_begin(), E = D->chain_end(); I != E; ++I) {
527 NamedDecl *VD = const_cast<NamedDecl*>(*I);
528 push(isa<VarDecl>(VD) ? "variable" : "field");
529 setPointer("ptr", VD);
530 completeAttrs();
531 pop();
532 }
533 }
534
535 // TypeDecl
536 void visitTypeDeclAttrs(TypeDecl *D) {
537 setPointer("typeptr", D->getTypeForDecl());
538 }
539
540 // TypedefDecl
541 void visitTypedefDeclAttrs(TypedefDecl *D) {
542 visitRedeclarableAttrs(D);
543 }
544 void visitTypedefDeclChildren(TypedefDecl *D) {
545 dispatch(D->getTypeSourceInfo()->getTypeLoc());
546 }
547
548 // TagDecl
549 void visitTagDeclAttrs(TagDecl *D) {
550 visitRedeclarableAttrs(D);
551 }
552 void visitTagDeclAsContext(TagDecl *D) {
553 visitDeclContext(D);
554 }
555
556 // EnumDecl
557 void visitEnumDeclAttrs(EnumDecl *D) {
558 setFlag("scoped", D->isScoped());
559 setFlag("fixed", D->isFixed());
560 }
561 void visitEnumDeclChildren(EnumDecl *D) {
562 {
563 TemporaryContainer C(*this, "promotion_type");
564 dispatch(D->getPromotionType());
565 }
566 {
567 TemporaryContainer C(*this, "integer_type");
568 dispatch(D->getIntegerType());
569 }
570 }
571
572 // RecordDecl ?
573
574 void visitCXXRecordDeclChildren(CXXRecordDecl *D) {
575 if (!D->isThisDeclarationADefinition()) return;
576
577 for (CXXRecordDecl::base_class_iterator
578 I = D->bases_begin(), E = D->bases_end(); I != E; ++I) {
579 push("base");
580 setAccess(I->getAccessSpecifier());
581 completeAttrs();
582 dispatch(I->getTypeSourceInfo()->getTypeLoc());
583 pop();
584 }
585 }
586
587 // ClassTemplateSpecializationDecl ?
588
589 // FileScopeAsmDecl ?
590
591 // BlockDecl
592 void visitBlockDeclAttrs(BlockDecl *D) {
593 setFlag("variadic", D->isVariadic());
594 }
595 void visitBlockDeclChildren(BlockDecl *D) {
596 for (FunctionDecl::param_iterator
597 I = D->param_begin(), E = D->param_end(); I != E; ++I)
598 dispatch(*I);
599 dispatch(D->getBody());
600 }
601
602 // AccessSpecDecl
603 void visitAccessSpecDeclAttrs(AccessSpecDecl *D) {
604 setAccess(D->getAccess());
605 }
606
607 // TemplateDecl
608 void visitTemplateDeclChildren(TemplateDecl *D) {
609 visitTemplateParameters(D->getTemplateParameters());
610 dispatch(D->getTemplatedDecl());
611 }
612
613 // FunctionTemplateDecl
614 void visitFunctionTemplateDeclAttrs(FunctionTemplateDecl *D) {
615 visitRedeclarableAttrs(D);
616 }
617 void visitFunctionTemplateDeclChildren(FunctionTemplateDecl *D) {
618 // Mention all the specializations which don't have explicit
619 // declarations elsewhere.
620 for (FunctionTemplateDecl::spec_iterator
621 I = D->spec_begin(), E = D->spec_end(); I != E; ++I) {
622 FunctionTemplateSpecializationInfo *Info
623 = I->getTemplateSpecializationInfo();
624
625 bool Unknown = false;
626 switch (Info->getTemplateSpecializationKind()) {
627 case TSK_ImplicitInstantiation: Unknown = false; break;
628 case TSK_Undeclared: Unknown = true; break;
629
630 // These will be covered at their respective sites.
631 case TSK_ExplicitSpecialization: continue;
632 case TSK_ExplicitInstantiationDeclaration: continue;
633 case TSK_ExplicitInstantiationDefinition: continue;
634 }
635
636 TemporaryContainer C(*this,
637 Unknown ? "uninstantiated" : "instantiation");
638 visitTemplateArguments(*Info->TemplateArguments);
639 dispatch(Info->Function);
640 }
641 }
642
643 // ClasTemplateDecl
644 void visitClassTemplateDeclAttrs(ClassTemplateDecl *D) {
645 visitRedeclarableAttrs(D);
646 }
647 void visitClassTemplateDeclChildren(ClassTemplateDecl *D) {
648 // Mention all the specializations which don't have explicit
649 // declarations elsewhere.
650 for (ClassTemplateDecl::spec_iterator
651 I = D->spec_begin(), E = D->spec_end(); I != E; ++I) {
652
653 bool Unknown = false;
654 switch (I->getTemplateSpecializationKind()) {
655 case TSK_ImplicitInstantiation: Unknown = false; break;
656 case TSK_Undeclared: Unknown = true; break;
657
658 // These will be covered at their respective sites.
659 case TSK_ExplicitSpecialization: continue;
660 case TSK_ExplicitInstantiationDeclaration: continue;
661 case TSK_ExplicitInstantiationDefinition: continue;
662 }
663
664 TemporaryContainer C(*this,
665 Unknown ? "uninstantiated" : "instantiation");
666 visitTemplateArguments(I->getTemplateArgs());
667 dispatch(*I);
668 }
669 }
670
671 // TemplateTypeParmDecl
672 void visitTemplateTypeParmDeclAttrs(TemplateTypeParmDecl *D) {
673 setInteger("depth", D->getDepth());
674 setInteger("index", D->getIndex());
675 }
676 void visitTemplateTypeParmDeclChildren(TemplateTypeParmDecl *D) {
677 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
678 dispatch(D->getDefaultArgumentInfo()->getTypeLoc());
679 // parameter pack?
680 }
681
682 // NonTypeTemplateParmDecl
683 void visitNonTypeTemplateParmDeclAttrs(NonTypeTemplateParmDecl *D) {
684 setInteger("depth", D->getDepth());
685 setInteger("index", D->getIndex());
686 }
687 void visitNonTypeTemplateParmDeclChildren(NonTypeTemplateParmDecl *D) {
688 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
689 dispatch(D->getDefaultArgument());
690 // parameter pack?
691 }
692
693 // TemplateTemplateParmDecl
694 void visitTemplateTemplateParmDeclAttrs(TemplateTemplateParmDecl *D) {
695 setInteger("depth", D->getDepth());
696 setInteger("index", D->getIndex());
697 }
698 void visitTemplateTemplateParmDeclChildren(TemplateTemplateParmDecl *D) {
699 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
700 dispatch(D->getDefaultArgument());
701 // parameter pack?
702 }
703
704 // FriendDecl
705 void visitFriendDeclChildren(FriendDecl *D) {
706 if (TypeSourceInfo *T = D->getFriendType())
707 dispatch(T->getTypeLoc());
708 else
709 dispatch(D->getFriendDecl());
710 }
711
712 // UsingDirectiveDecl ?
713 // UsingDecl ?
714 // UsingShadowDecl ?
715 // NamespaceAliasDecl ?
716 // UnresolvedUsingValueDecl ?
717 // UnresolvedUsingTypenameDecl ?
718 // StaticAssertDecl ?
719
John McCall3bddf5c2010-12-02 10:24:56 +0000720 // ObjCImplDecl
721 void visitObjCImplDeclChildren(ObjCImplDecl *D) {
722 visitDeclRef(D->getClassInterface());
723 }
724 void visitObjCImplDeclAsContext(ObjCImplDecl *D) {
725 visitDeclContext(D);
726 }
727
728 // ObjCClassDecl
729 void visitObjCClassDeclChildren(ObjCClassDecl *D) {
730 for (ObjCClassDecl::iterator I = D->begin(), E = D->end(); I != E; ++I)
731 visitDeclRef(I->getInterface());
732 }
733
734 // ObjCInterfaceDecl
735 void visitCategoryList(ObjCCategoryDecl *D) {
736 if (!D) return;
737
738 TemporaryContainer C(*this, "categories");
739 for (; D; D = D->getNextClassCategory())
740 visitDeclRef(D);
741 }
742 void visitObjCInterfaceDeclAttrs(ObjCInterfaceDecl *D) {
743 setPointer("typeptr", D->getTypeForDecl());
744 setFlag("forward_decl", D->isForwardDecl());
745 setFlag("implicit_interface", D->isImplicitInterfaceDecl());
746 }
747 void visitObjCInterfaceDeclChildren(ObjCInterfaceDecl *D) {
748 visitDeclRef("super", D->getSuperClass());
749 visitDeclRef("implementation", D->getImplementation());
750 if (D->protocol_begin() != D->protocol_end()) {
751 TemporaryContainer C(*this, "protocols");
752 for (ObjCInterfaceDecl::protocol_iterator
753 I = D->protocol_begin(), E = D->protocol_end(); I != E; ++I)
754 visitDeclRef(*I);
755 }
756 visitCategoryList(D->getCategoryList());
757 }
758 void visitObjCInterfaceDeclAsContext(ObjCInterfaceDecl *D) {
759 visitDeclContext(D);
760 }
761
762 // ObjCCategoryDecl
763 void visitObjCCategoryDeclAttrs(ObjCCategoryDecl *D) {
764 setFlag("extension", D->IsClassExtension());
765 setFlag("synth_bitfield", D->hasSynthBitfield());
766 }
767 void visitObjCCategoryDeclChildren(ObjCCategoryDecl *D) {
768 visitDeclRef("interface", D->getClassInterface());
769 visitDeclRef("implementation", D->getImplementation());
770 if (D->protocol_begin() != D->protocol_end()) {
771 TemporaryContainer C(*this, "protocols");
772 for (ObjCCategoryDecl::protocol_iterator
773 I = D->protocol_begin(), E = D->protocol_end(); I != E; ++I)
774 visitDeclRef(*I);
775 }
776 }
777 void visitObjCCategoryDeclAsContext(ObjCCategoryDecl *D) {
778 visitDeclContext(D);
779 }
780
781 // ObjCCategoryImplDecl
782 void visitObjCCategoryImplDeclAttrs(ObjCCategoryImplDecl *D) {
783 set("identifier", D->getName());
784 }
785 void visitObjCCategoryImplDeclChildren(ObjCCategoryImplDecl *D) {
786 visitDeclRef(D->getCategoryDecl());
787 }
788
789 // ObjCImplementationDecl
790 void visitObjCImplementationDeclAttrs(ObjCImplementationDecl *D) {
791 setFlag("synth_bitfield", D->hasSynthBitfield());
792 set("identifier", D->getName());
793 }
794 void visitObjCImplementationDeclChildren(ObjCImplementationDecl *D) {
795 visitDeclRef("super", D->getSuperClass());
796 if (D->init_begin() != D->init_end()) {
797 TemporaryContainer C(*this, "initializers");
798 for (ObjCImplementationDecl::init_iterator
799 I = D->init_begin(), E = D->init_end(); I != E; ++I)
800 dispatch(*I);
801 }
802 }
803
804 // ObjCForwardProtocolDecl
805 void visitObjCForwardProtocolDeclChildren(ObjCForwardProtocolDecl *D) {
806 for (ObjCForwardProtocolDecl::protocol_iterator
807 I = D->protocol_begin(), E = D->protocol_end(); I != E; ++I)
808 visitDeclRef(*I);
809 }
810
811 // ObjCProtocolDecl
812 void visitObjCProtocolDeclAttrs(ObjCProtocolDecl *D) {
813 setFlag("forward_decl", D->isForwardDecl());
814 }
815 void visitObjCProtocolDeclChildren(ObjCProtocolDecl *D) {
816 if (D->protocol_begin() != D->protocol_end()) {
817 TemporaryContainer C(*this, "protocols");
818 for (ObjCInterfaceDecl::protocol_iterator
819 I = D->protocol_begin(), E = D->protocol_end(); I != E; ++I)
820 visitDeclRef(*I);
821 }
822 }
823 void visitObjCProtocolDeclAsContext(ObjCProtocolDecl *D) {
824 visitDeclContext(D);
825 }
826
827 // ObjCMethodDecl
828 void visitObjCMethodDeclAttrs(ObjCMethodDecl *D) {
829 // decl qualifier?
830 // implementation control?
831
832 setFlag("instance", D->isInstanceMethod());
833 setFlag("variadic", D->isVariadic());
834 setFlag("synthesized", D->isSynthesized());
835 setFlag("defined", D->isDefined());
836 }
837 void visitObjCMethodDeclChildren(ObjCMethodDecl *D) {
838 dispatch(D->getResultType());
839 for (ObjCMethodDecl::param_iterator
840 I = D->param_begin(), E = D->param_end(); I != E; ++I)
841 dispatch(*I);
842 if (D->isThisDeclarationADefinition())
843 dispatch(D->getBody());
844 }
845
846 // ObjCIvarDecl
847 void setAccessControl(llvm::StringRef prop, ObjCIvarDecl::AccessControl AC) {
848 switch (AC) {
849 case ObjCIvarDecl::None: return set(prop, "none");
850 case ObjCIvarDecl::Private: return set(prop, "private");
851 case ObjCIvarDecl::Protected: return set(prop, "protected");
852 case ObjCIvarDecl::Public: return set(prop, "public");
853 case ObjCIvarDecl::Package: return set(prop, "package");
854 }
855 }
856 void visitObjCIvarDeclAttrs(ObjCIvarDecl *D) {
857 setFlag("synthesize", D->getSynthesize());
858 setAccessControl("access", D->getAccessControl());
859 }
860
861 // ObjCCompatibleAliasDecl
862 void visitObjCCompatibleAliasDeclChildren(ObjCCompatibleAliasDecl *D) {
863 visitDeclRef(D->getClassInterface());
864 }
865
866 // FIXME: ObjCPropertyDecl
867 // FIXME: ObjCPropertyImplDecl
868
John McCall26c25c92010-11-24 11:30:07 +0000869 //---- Types -----------------------------------------------------//
870 void dispatch(TypeLoc TL) {
871 dispatch(TL.getType()); // for now
872 }
873
874 void dispatch(QualType T) {
875 if (T.hasLocalQualifiers()) {
876 push("QualType");
877 Qualifiers Qs = T.getLocalQualifiers();
878 setFlag("const", Qs.hasConst());
879 setFlag("volatile", Qs.hasVolatile());
880 setFlag("restrict", Qs.hasRestrict());
881 if (Qs.hasAddressSpace()) setInteger("addrspace", Qs.getAddressSpace());
882 if (Qs.hasObjCGCAttr()) {
883 switch (Qs.getObjCGCAttr()) {
884 case Qualifiers::Weak: set("gc", "weak"); break;
885 case Qualifiers::Strong: set("gc", "strong"); break;
886 case Qualifiers::GCNone: llvm_unreachable("explicit none");
887 }
888 }
889
890 completeAttrs();
891 dispatch(QualType(T.getTypePtr(), 0));
892 pop();
893 return;
894 }
895
896 Type *Ty = const_cast<Type*>(T.getTypePtr());
897 push(getTypeKindName(Ty));
John McCall13cf5e22010-11-24 11:53:13 +0000898 XMLTypeVisitor<XMLDumper>::dispatch(const_cast<Type*>(T.getTypePtr()));
John McCall26c25c92010-11-24 11:30:07 +0000899 pop();
900 }
901
902 void setCallingConv(CallingConv CC) {
903 switch (CC) {
904 case CC_Default: return;
905 case CC_C: return set("cc", "cdecl");
906 case CC_X86FastCall: return set("cc", "x86_fastcall");
907 case CC_X86StdCall: return set("cc", "x86_stdcall");
908 case CC_X86ThisCall: return set("cc", "x86_thiscall");
909 case CC_X86Pascal: return set("cc", "x86_pascal");
910 }
911 }
912
913 void visitTypeAttrs(Type *D) {
914 setPointer(D);
915 setFlag("dependent", D->isDependentType());
916 setFlag("variably_modified", D->isVariablyModifiedType());
917
918 setPointer("canonical", D->getCanonicalTypeInternal().getAsOpaquePtr());
919 }
920
921 void visitPointerTypeChildren(PointerType *T) {
922 dispatch(T->getPointeeType());
923 }
924 void visitReferenceTypeChildren(ReferenceType *T) {
925 dispatch(T->getPointeeType());
926 }
927 void visitObjCObjectPointerTypeChildren(ObjCObjectPointerType *T) {
928 dispatch(T->getPointeeType());
929 }
930 void visitBlockPointerTypeChildren(BlockPointerType *T) {
931 dispatch(T->getPointeeType());
932 }
933
934 // Types that just wrap declarations.
935 void visitTagTypeChildren(TagType *T) {
936 visitDeclRef(T->getDecl());
937 }
938 void visitTypedefTypeChildren(TypedefType *T) {
939 visitDeclRef(T->getDecl());
940 }
941 void visitObjCInterfaceTypeChildren(ObjCInterfaceType *T) {
942 visitDeclRef(T->getDecl());
943 }
944 void visitUnresolvedUsingTypeChildren(UnresolvedUsingType *T) {
945 visitDeclRef(T->getDecl());
946 }
947 void visitInjectedClassNameTypeChildren(InjectedClassNameType *T) {
948 visitDeclRef(T->getDecl());
949 }
950
951 void visitFunctionTypeAttrs(FunctionType *T) {
952 setFlag("noreturn", T->getNoReturnAttr());
953 setCallingConv(T->getCallConv());
954 if (T->getRegParmType()) setInteger("regparm", T->getRegParmType());
955 }
956 void visitFunctionTypeChildren(FunctionType *T) {
957 dispatch(T->getResultType());
958 }
959
960 void visitFunctionProtoTypeAttrs(FunctionProtoType *T) {
961 setFlag("const", T->getTypeQuals() & Qualifiers::Const);
962 setFlag("volatile", T->getTypeQuals() & Qualifiers::Volatile);
963 setFlag("restrict", T->getTypeQuals() & Qualifiers::Restrict);
964 }
965 void visitFunctionProtoTypeChildren(FunctionProtoType *T) {
966 push("parameters");
967 setFlag("variadic", T->isVariadic());
968 completeAttrs();
969 for (FunctionProtoType::arg_type_iterator
970 I = T->arg_type_begin(), E = T->arg_type_end(); I != E; ++I)
971 dispatch(*I);
972 pop();
973
974 if (T->hasExceptionSpec()) {
975 push("exception_specifiers");
976 setFlag("any", T->hasAnyExceptionSpec());
977 completeAttrs();
978 for (FunctionProtoType::exception_iterator
979 I = T->exception_begin(), E = T->exception_end(); I != E; ++I)
980 dispatch(*I);
981 pop();
982 }
983 }
984
985 void visitTemplateSpecializationTypeChildren(TemplateSpecializationType *T) {
986 if (const RecordType *RT = T->getAs<RecordType>())
987 visitDeclRef(RT->getDecl());
988
989 // TODO: TemplateName
990
991 push("template_arguments");
992 completeAttrs();
993 for (unsigned I = 0, E = T->getNumArgs(); I != E; ++I)
994 dispatch(T->getArg(I));
995 pop();
996 }
997
998 //---- Statements ------------------------------------------------//
999 void dispatch(Stmt *S) {
1000 // FIXME: this is not really XML at all
1001 push("Stmt");
John McCall3bddf5c2010-12-02 10:24:56 +00001002 out << ">\n";
John McCall26c25c92010-11-24 11:30:07 +00001003 Stack.back().State = NS_Children; // explicitly become non-lazy
1004 S->dump(out, Context.getSourceManager());
1005 out << '\n';
1006 pop();
1007 }
1008};
1009}
1010
1011void Decl::dumpXML() const {
1012 dumpXML(llvm::errs());
1013}
1014
1015void Decl::dumpXML(llvm::raw_ostream &out) const {
1016 XMLDumper(out, getASTContext()).dispatch(const_cast<Decl*>(this));
1017}
1018
1019#else /* ifndef NDEBUG */
1020
1021void Decl::dumpXML() const {}
1022void Decl::dumpXML(llvm::raw_ostream &out) const {}
1023
1024#endif