blob: 3cfff31910afaa98c6dec9b7369dab19d0368537 [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:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000322 case TemplateArgument::TemplateExpansion:
323 // FIXME: Implement!
324 break;
325
John McCall26c25c92010-11-24 11:30:07 +0000326 case TemplateArgument::Declaration: {
327 visitDeclRef(A.getAsDecl());
328 break;
329 }
330 case TemplateArgument::Integral: {
331 push("integer");
332 setInteger("value", *A.getAsIntegral());
333 completeAttrs();
334 pop();
335 break;
336 }
337 case TemplateArgument::Expression: {
338 dispatch(A.getAsExpr());
339 break;
340 }
341 case TemplateArgument::Pack: {
Douglas Gregor87dd6972010-12-20 16:52:59 +0000342 for (TemplateArgument::pack_iterator P = A.pack_begin(),
343 PEnd = A.pack_end();
344 P != PEnd; ++P)
345 dispatch(*P);
John McCall26c25c92010-11-24 11:30:07 +0000346 break;
347 }
348 }
349 }
350
351 void dispatch(const TemplateArgumentLoc &A) {
352 dispatch(A.getArgument());
353 }
354
355 //---- Declarations ------------------------------------------------//
356 // Calls are made in this order:
357 // # Enter a new node.
358 // push("FieldDecl")
359 //
360 // # In this phase, attributes are set on the node.
361 // visitDeclAttrs(D)
362 // visitNamedDeclAttrs(D)
363 // ...
364 // visitFieldDeclAttrs(D)
365 //
366 // # No more attributes after this point.
367 // completeAttrs()
368 //
369 // # Create "header" child nodes, i.e. those which logically
370 // # belong to the declaration itself.
371 // visitDeclChildren(D)
372 // visitNamedDeclChildren(D)
373 // ...
374 // visitFieldDeclChildren(D)
375 //
376 // # Create nodes for the lexical children.
377 // visitDeclAsContext(D)
378 // visitNamedDeclAsContext(D)
379 // ...
380 // visitFieldDeclAsContext(D)
381 //
382 // # Finish the node.
383 // pop();
384 void dispatch(Decl *D) {
385 push(D->getDeclKindName());
John McCall13cf5e22010-11-24 11:53:13 +0000386 XMLDeclVisitor<XMLDumper>::dispatch(D);
John McCall26c25c92010-11-24 11:30:07 +0000387 pop();
388 }
389 void visitDeclAttrs(Decl *D) {
390 setPointer(D);
391 }
392
393 /// Visit all the lexical decls in the given context.
394 void visitDeclContext(DeclContext *DC) {
395 for (DeclContext::decl_iterator
396 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
397 dispatch(*I);
398
399 // FIXME: point out visible declarations not in lexical context?
400 }
401
402 /// Set the "access" attribute on the current node according to the
403 /// given specifier.
404 void setAccess(AccessSpecifier AS) {
405 switch (AS) {
406 case AS_public: return set("access", "public");
407 case AS_protected: return set("access", "protected");
408 case AS_private: return set("access", "private");
409 case AS_none: llvm_unreachable("explicit forbidden access");
410 }
411 }
412
413 template <class T> void visitRedeclarableAttrs(T *D) {
414 if (T *Prev = D->getPreviousDeclaration())
415 setPointer("previous", Prev);
416 }
417
418
419 // TranslationUnitDecl
420 void visitTranslationUnitDeclAsContext(TranslationUnitDecl *D) {
421 visitDeclContext(D);
422 }
423
424 // LinkageSpecDecl
425 void visitLinkageSpecDeclAttrs(LinkageSpecDecl *D) {
426 llvm::StringRef lang = "";
427 switch (D->getLanguage()) {
428 case LinkageSpecDecl::lang_c: lang = "C"; break;
429 case LinkageSpecDecl::lang_cxx: lang = "C++"; break;
430 }
431 set("lang", lang);
432 }
433 void visitLinkageSpecDeclAsContext(LinkageSpecDecl *D) {
434 visitDeclContext(D);
435 }
436
437 // NamespaceDecl
438 void visitNamespaceDeclAttrs(NamespaceDecl *D) {
439 setFlag("inline", D->isInline());
440 if (!D->isOriginalNamespace())
441 setPointer("original", D->getOriginalNamespace());
442 }
443 void visitNamespaceDeclAsContext(NamespaceDecl *D) {
444 visitDeclContext(D);
445 }
446
447 // NamedDecl
448 void visitNamedDeclAttrs(NamedDecl *D) {
449 setName(D->getDeclName());
450 }
451
452 // ValueDecl
453 void visitValueDeclChildren(ValueDecl *D) {
454 dispatch(D->getType());
455 }
456
457 // DeclaratorDecl
458 void visitDeclaratorDeclChildren(DeclaratorDecl *D) {
459 //dispatch(D->getTypeSourceInfo()->getTypeLoc());
460 }
461
462 // VarDecl
463 void visitVarDeclAttrs(VarDecl *D) {
464 visitRedeclarableAttrs(D);
465 if (D->getStorageClass() != SC_None)
466 set("storage",
467 VarDecl::getStorageClassSpecifierString(D->getStorageClass()));
468 setFlag("directinit", D->hasCXXDirectInitializer());
469 setFlag("nrvo", D->isNRVOVariable());
470 // TODO: instantiation, etc.
471 }
472 void visitVarDeclChildren(VarDecl *D) {
473 if (D->hasInit()) dispatch(D->getInit());
474 }
475
476 // ParmVarDecl?
477
478 // FunctionDecl
479 void visitFunctionDeclAttrs(FunctionDecl *D) {
480 visitRedeclarableAttrs(D);
481 setFlag("pure", D->isPure());
482 setFlag("trivial", D->isTrivial());
483 setFlag("returnzero", D->hasImplicitReturnZero());
484 setFlag("prototype", D->hasWrittenPrototype());
485 setFlag("deleted", D->isDeleted());
486 if (D->getStorageClass() != SC_None)
487 set("storage",
488 VarDecl::getStorageClassSpecifierString(D->getStorageClass()));
489 setFlag("inline", D->isInlineSpecified());
490 // TODO: instantiation, etc.
491 }
492 void visitFunctionDeclChildren(FunctionDecl *D) {
493 for (FunctionDecl::param_iterator
494 I = D->param_begin(), E = D->param_end(); I != E; ++I)
495 dispatch(*I);
496 if (D->isThisDeclarationADefinition())
497 dispatch(D->getBody());
498 }
499
500 // CXXMethodDecl ?
501 // CXXConstructorDecl ?
502 // CXXDestructorDecl ?
503 // CXXConversionDecl ?
504
Sean Huntcbb67482011-01-08 20:30:50 +0000505 void dispatch(CXXCtorInitializer *Init) {
John McCall3bddf5c2010-12-02 10:24:56 +0000506 // TODO
507 }
508
John McCall26c25c92010-11-24 11:30:07 +0000509 // FieldDecl
510 void visitFieldDeclAttrs(FieldDecl *D) {
511 setFlag("mutable", D->isMutable());
512 }
513 void visitFieldDeclChildren(FieldDecl *D) {
514 if (D->isBitField()) {
515 TemporaryContainer C(*this, "bitwidth");
516 dispatch(D->getBitWidth());
517 }
518 // TODO: C++0x member initializer
519 }
520
521 // EnumConstantDecl
522 void visitEnumConstantDeclChildren(EnumConstantDecl *D) {
523 // value in any case?
524 if (D->getInitExpr()) dispatch(D->getInitExpr());
525 }
526
527 // IndirectFieldDecl
528 void visitIndirectFieldDeclChildren(IndirectFieldDecl *D) {
529 for (IndirectFieldDecl::chain_iterator
530 I = D->chain_begin(), E = D->chain_end(); I != E; ++I) {
531 NamedDecl *VD = const_cast<NamedDecl*>(*I);
532 push(isa<VarDecl>(VD) ? "variable" : "field");
533 setPointer("ptr", VD);
534 completeAttrs();
535 pop();
536 }
537 }
538
539 // TypeDecl
540 void visitTypeDeclAttrs(TypeDecl *D) {
541 setPointer("typeptr", D->getTypeForDecl());
542 }
543
544 // TypedefDecl
545 void visitTypedefDeclAttrs(TypedefDecl *D) {
546 visitRedeclarableAttrs(D);
547 }
548 void visitTypedefDeclChildren(TypedefDecl *D) {
549 dispatch(D->getTypeSourceInfo()->getTypeLoc());
550 }
551
552 // TagDecl
553 void visitTagDeclAttrs(TagDecl *D) {
554 visitRedeclarableAttrs(D);
555 }
556 void visitTagDeclAsContext(TagDecl *D) {
557 visitDeclContext(D);
558 }
559
560 // EnumDecl
561 void visitEnumDeclAttrs(EnumDecl *D) {
562 setFlag("scoped", D->isScoped());
563 setFlag("fixed", D->isFixed());
564 }
565 void visitEnumDeclChildren(EnumDecl *D) {
566 {
567 TemporaryContainer C(*this, "promotion_type");
568 dispatch(D->getPromotionType());
569 }
570 {
571 TemporaryContainer C(*this, "integer_type");
572 dispatch(D->getIntegerType());
573 }
574 }
575
576 // RecordDecl ?
577
578 void visitCXXRecordDeclChildren(CXXRecordDecl *D) {
579 if (!D->isThisDeclarationADefinition()) return;
580
581 for (CXXRecordDecl::base_class_iterator
582 I = D->bases_begin(), E = D->bases_end(); I != E; ++I) {
583 push("base");
584 setAccess(I->getAccessSpecifier());
585 completeAttrs();
586 dispatch(I->getTypeSourceInfo()->getTypeLoc());
587 pop();
588 }
589 }
590
591 // ClassTemplateSpecializationDecl ?
592
593 // FileScopeAsmDecl ?
594
595 // BlockDecl
596 void visitBlockDeclAttrs(BlockDecl *D) {
597 setFlag("variadic", D->isVariadic());
598 }
599 void visitBlockDeclChildren(BlockDecl *D) {
600 for (FunctionDecl::param_iterator
601 I = D->param_begin(), E = D->param_end(); I != E; ++I)
602 dispatch(*I);
603 dispatch(D->getBody());
604 }
605
606 // AccessSpecDecl
607 void visitAccessSpecDeclAttrs(AccessSpecDecl *D) {
608 setAccess(D->getAccess());
609 }
610
611 // TemplateDecl
612 void visitTemplateDeclChildren(TemplateDecl *D) {
613 visitTemplateParameters(D->getTemplateParameters());
614 dispatch(D->getTemplatedDecl());
615 }
616
617 // FunctionTemplateDecl
618 void visitFunctionTemplateDeclAttrs(FunctionTemplateDecl *D) {
619 visitRedeclarableAttrs(D);
620 }
621 void visitFunctionTemplateDeclChildren(FunctionTemplateDecl *D) {
622 // Mention all the specializations which don't have explicit
623 // declarations elsewhere.
624 for (FunctionTemplateDecl::spec_iterator
625 I = D->spec_begin(), E = D->spec_end(); I != E; ++I) {
626 FunctionTemplateSpecializationInfo *Info
627 = I->getTemplateSpecializationInfo();
628
629 bool Unknown = false;
630 switch (Info->getTemplateSpecializationKind()) {
631 case TSK_ImplicitInstantiation: Unknown = false; break;
632 case TSK_Undeclared: Unknown = true; break;
633
634 // These will be covered at their respective sites.
635 case TSK_ExplicitSpecialization: continue;
636 case TSK_ExplicitInstantiationDeclaration: continue;
637 case TSK_ExplicitInstantiationDefinition: continue;
638 }
639
640 TemporaryContainer C(*this,
641 Unknown ? "uninstantiated" : "instantiation");
642 visitTemplateArguments(*Info->TemplateArguments);
643 dispatch(Info->Function);
644 }
645 }
646
647 // ClasTemplateDecl
648 void visitClassTemplateDeclAttrs(ClassTemplateDecl *D) {
649 visitRedeclarableAttrs(D);
650 }
651 void visitClassTemplateDeclChildren(ClassTemplateDecl *D) {
652 // Mention all the specializations which don't have explicit
653 // declarations elsewhere.
654 for (ClassTemplateDecl::spec_iterator
655 I = D->spec_begin(), E = D->spec_end(); I != E; ++I) {
656
657 bool Unknown = false;
658 switch (I->getTemplateSpecializationKind()) {
659 case TSK_ImplicitInstantiation: Unknown = false; break;
660 case TSK_Undeclared: Unknown = true; break;
661
662 // These will be covered at their respective sites.
663 case TSK_ExplicitSpecialization: continue;
664 case TSK_ExplicitInstantiationDeclaration: continue;
665 case TSK_ExplicitInstantiationDefinition: continue;
666 }
667
668 TemporaryContainer C(*this,
669 Unknown ? "uninstantiated" : "instantiation");
670 visitTemplateArguments(I->getTemplateArgs());
671 dispatch(*I);
672 }
673 }
674
675 // TemplateTypeParmDecl
676 void visitTemplateTypeParmDeclAttrs(TemplateTypeParmDecl *D) {
677 setInteger("depth", D->getDepth());
678 setInteger("index", D->getIndex());
679 }
680 void visitTemplateTypeParmDeclChildren(TemplateTypeParmDecl *D) {
681 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
682 dispatch(D->getDefaultArgumentInfo()->getTypeLoc());
683 // parameter pack?
684 }
685
686 // NonTypeTemplateParmDecl
687 void visitNonTypeTemplateParmDeclAttrs(NonTypeTemplateParmDecl *D) {
688 setInteger("depth", D->getDepth());
689 setInteger("index", D->getIndex());
690 }
691 void visitNonTypeTemplateParmDeclChildren(NonTypeTemplateParmDecl *D) {
692 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
693 dispatch(D->getDefaultArgument());
694 // parameter pack?
695 }
696
697 // TemplateTemplateParmDecl
698 void visitTemplateTemplateParmDeclAttrs(TemplateTemplateParmDecl *D) {
699 setInteger("depth", D->getDepth());
700 setInteger("index", D->getIndex());
701 }
702 void visitTemplateTemplateParmDeclChildren(TemplateTemplateParmDecl *D) {
703 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
704 dispatch(D->getDefaultArgument());
705 // parameter pack?
706 }
707
708 // FriendDecl
709 void visitFriendDeclChildren(FriendDecl *D) {
710 if (TypeSourceInfo *T = D->getFriendType())
711 dispatch(T->getTypeLoc());
712 else
713 dispatch(D->getFriendDecl());
714 }
715
716 // UsingDirectiveDecl ?
717 // UsingDecl ?
718 // UsingShadowDecl ?
719 // NamespaceAliasDecl ?
720 // UnresolvedUsingValueDecl ?
721 // UnresolvedUsingTypenameDecl ?
722 // StaticAssertDecl ?
723
John McCall3bddf5c2010-12-02 10:24:56 +0000724 // ObjCImplDecl
725 void visitObjCImplDeclChildren(ObjCImplDecl *D) {
726 visitDeclRef(D->getClassInterface());
727 }
728 void visitObjCImplDeclAsContext(ObjCImplDecl *D) {
729 visitDeclContext(D);
730 }
731
732 // ObjCClassDecl
733 void visitObjCClassDeclChildren(ObjCClassDecl *D) {
734 for (ObjCClassDecl::iterator I = D->begin(), E = D->end(); I != E; ++I)
735 visitDeclRef(I->getInterface());
736 }
737
738 // ObjCInterfaceDecl
739 void visitCategoryList(ObjCCategoryDecl *D) {
740 if (!D) return;
741
742 TemporaryContainer C(*this, "categories");
743 for (; D; D = D->getNextClassCategory())
744 visitDeclRef(D);
745 }
746 void visitObjCInterfaceDeclAttrs(ObjCInterfaceDecl *D) {
747 setPointer("typeptr", D->getTypeForDecl());
748 setFlag("forward_decl", D->isForwardDecl());
749 setFlag("implicit_interface", D->isImplicitInterfaceDecl());
750 }
751 void visitObjCInterfaceDeclChildren(ObjCInterfaceDecl *D) {
752 visitDeclRef("super", D->getSuperClass());
753 visitDeclRef("implementation", D->getImplementation());
754 if (D->protocol_begin() != D->protocol_end()) {
755 TemporaryContainer C(*this, "protocols");
756 for (ObjCInterfaceDecl::protocol_iterator
757 I = D->protocol_begin(), E = D->protocol_end(); I != E; ++I)
758 visitDeclRef(*I);
759 }
760 visitCategoryList(D->getCategoryList());
761 }
762 void visitObjCInterfaceDeclAsContext(ObjCInterfaceDecl *D) {
763 visitDeclContext(D);
764 }
765
766 // ObjCCategoryDecl
767 void visitObjCCategoryDeclAttrs(ObjCCategoryDecl *D) {
768 setFlag("extension", D->IsClassExtension());
769 setFlag("synth_bitfield", D->hasSynthBitfield());
770 }
771 void visitObjCCategoryDeclChildren(ObjCCategoryDecl *D) {
772 visitDeclRef("interface", D->getClassInterface());
773 visitDeclRef("implementation", D->getImplementation());
774 if (D->protocol_begin() != D->protocol_end()) {
775 TemporaryContainer C(*this, "protocols");
776 for (ObjCCategoryDecl::protocol_iterator
777 I = D->protocol_begin(), E = D->protocol_end(); I != E; ++I)
778 visitDeclRef(*I);
779 }
780 }
781 void visitObjCCategoryDeclAsContext(ObjCCategoryDecl *D) {
782 visitDeclContext(D);
783 }
784
785 // ObjCCategoryImplDecl
786 void visitObjCCategoryImplDeclAttrs(ObjCCategoryImplDecl *D) {
787 set("identifier", D->getName());
788 }
789 void visitObjCCategoryImplDeclChildren(ObjCCategoryImplDecl *D) {
790 visitDeclRef(D->getCategoryDecl());
791 }
792
793 // ObjCImplementationDecl
794 void visitObjCImplementationDeclAttrs(ObjCImplementationDecl *D) {
795 setFlag("synth_bitfield", D->hasSynthBitfield());
796 set("identifier", D->getName());
797 }
798 void visitObjCImplementationDeclChildren(ObjCImplementationDecl *D) {
799 visitDeclRef("super", D->getSuperClass());
800 if (D->init_begin() != D->init_end()) {
801 TemporaryContainer C(*this, "initializers");
802 for (ObjCImplementationDecl::init_iterator
803 I = D->init_begin(), E = D->init_end(); I != E; ++I)
804 dispatch(*I);
805 }
806 }
807
808 // ObjCForwardProtocolDecl
809 void visitObjCForwardProtocolDeclChildren(ObjCForwardProtocolDecl *D) {
810 for (ObjCForwardProtocolDecl::protocol_iterator
811 I = D->protocol_begin(), E = D->protocol_end(); I != E; ++I)
812 visitDeclRef(*I);
813 }
814
815 // ObjCProtocolDecl
816 void visitObjCProtocolDeclAttrs(ObjCProtocolDecl *D) {
817 setFlag("forward_decl", D->isForwardDecl());
818 }
819 void visitObjCProtocolDeclChildren(ObjCProtocolDecl *D) {
820 if (D->protocol_begin() != D->protocol_end()) {
821 TemporaryContainer C(*this, "protocols");
822 for (ObjCInterfaceDecl::protocol_iterator
823 I = D->protocol_begin(), E = D->protocol_end(); I != E; ++I)
824 visitDeclRef(*I);
825 }
826 }
827 void visitObjCProtocolDeclAsContext(ObjCProtocolDecl *D) {
828 visitDeclContext(D);
829 }
830
831 // ObjCMethodDecl
832 void visitObjCMethodDeclAttrs(ObjCMethodDecl *D) {
833 // decl qualifier?
834 // implementation control?
835
836 setFlag("instance", D->isInstanceMethod());
837 setFlag("variadic", D->isVariadic());
838 setFlag("synthesized", D->isSynthesized());
839 setFlag("defined", D->isDefined());
840 }
841 void visitObjCMethodDeclChildren(ObjCMethodDecl *D) {
842 dispatch(D->getResultType());
843 for (ObjCMethodDecl::param_iterator
844 I = D->param_begin(), E = D->param_end(); I != E; ++I)
845 dispatch(*I);
846 if (D->isThisDeclarationADefinition())
847 dispatch(D->getBody());
848 }
849
850 // ObjCIvarDecl
851 void setAccessControl(llvm::StringRef prop, ObjCIvarDecl::AccessControl AC) {
852 switch (AC) {
853 case ObjCIvarDecl::None: return set(prop, "none");
854 case ObjCIvarDecl::Private: return set(prop, "private");
855 case ObjCIvarDecl::Protected: return set(prop, "protected");
856 case ObjCIvarDecl::Public: return set(prop, "public");
857 case ObjCIvarDecl::Package: return set(prop, "package");
858 }
859 }
860 void visitObjCIvarDeclAttrs(ObjCIvarDecl *D) {
861 setFlag("synthesize", D->getSynthesize());
862 setAccessControl("access", D->getAccessControl());
863 }
864
865 // ObjCCompatibleAliasDecl
866 void visitObjCCompatibleAliasDeclChildren(ObjCCompatibleAliasDecl *D) {
867 visitDeclRef(D->getClassInterface());
868 }
869
870 // FIXME: ObjCPropertyDecl
871 // FIXME: ObjCPropertyImplDecl
872
John McCall26c25c92010-11-24 11:30:07 +0000873 //---- Types -----------------------------------------------------//
874 void dispatch(TypeLoc TL) {
875 dispatch(TL.getType()); // for now
876 }
877
878 void dispatch(QualType T) {
879 if (T.hasLocalQualifiers()) {
880 push("QualType");
881 Qualifiers Qs = T.getLocalQualifiers();
882 setFlag("const", Qs.hasConst());
883 setFlag("volatile", Qs.hasVolatile());
884 setFlag("restrict", Qs.hasRestrict());
885 if (Qs.hasAddressSpace()) setInteger("addrspace", Qs.getAddressSpace());
886 if (Qs.hasObjCGCAttr()) {
887 switch (Qs.getObjCGCAttr()) {
888 case Qualifiers::Weak: set("gc", "weak"); break;
889 case Qualifiers::Strong: set("gc", "strong"); break;
890 case Qualifiers::GCNone: llvm_unreachable("explicit none");
891 }
892 }
893
894 completeAttrs();
895 dispatch(QualType(T.getTypePtr(), 0));
896 pop();
897 return;
898 }
899
900 Type *Ty = const_cast<Type*>(T.getTypePtr());
901 push(getTypeKindName(Ty));
John McCall13cf5e22010-11-24 11:53:13 +0000902 XMLTypeVisitor<XMLDumper>::dispatch(const_cast<Type*>(T.getTypePtr()));
John McCall26c25c92010-11-24 11:30:07 +0000903 pop();
904 }
905
906 void setCallingConv(CallingConv CC) {
907 switch (CC) {
908 case CC_Default: return;
909 case CC_C: return set("cc", "cdecl");
910 case CC_X86FastCall: return set("cc", "x86_fastcall");
911 case CC_X86StdCall: return set("cc", "x86_stdcall");
912 case CC_X86ThisCall: return set("cc", "x86_thiscall");
913 case CC_X86Pascal: return set("cc", "x86_pascal");
914 }
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());
958 if (T->getRegParmType()) setInteger("regparm", T->getRegParmType());
959 }
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
978 if (T->hasExceptionSpec()) {
979 push("exception_specifiers");
980 setFlag("any", T->hasAnyExceptionSpec());
981 completeAttrs();
982 for (FunctionProtoType::exception_iterator
983 I = T->exception_begin(), E = T->exception_end(); I != E; ++I)
984 dispatch(*I);
985 pop();
986 }
987 }
988
989 void visitTemplateSpecializationTypeChildren(TemplateSpecializationType *T) {
990 if (const RecordType *RT = T->getAs<RecordType>())
991 visitDeclRef(RT->getDecl());
992
993 // TODO: TemplateName
994
995 push("template_arguments");
996 completeAttrs();
997 for (unsigned I = 0, E = T->getNumArgs(); I != E; ++I)
998 dispatch(T->getArg(I));
999 pop();
1000 }
1001
1002 //---- Statements ------------------------------------------------//
1003 void dispatch(Stmt *S) {
1004 // FIXME: this is not really XML at all
1005 push("Stmt");
John McCall3bddf5c2010-12-02 10:24:56 +00001006 out << ">\n";
John McCall26c25c92010-11-24 11:30:07 +00001007 Stack.back().State = NS_Children; // explicitly become non-lazy
1008 S->dump(out, Context.getSourceManager());
1009 out << '\n';
1010 pop();
1011 }
1012};
1013}
1014
1015void Decl::dumpXML() const {
1016 dumpXML(llvm::errs());
1017}
1018
1019void Decl::dumpXML(llvm::raw_ostream &out) const {
1020 XMLDumper(out, getASTContext()).dispatch(const_cast<Decl*>(this));
1021}
1022
1023#else /* ifndef NDEBUG */
1024
1025void Decl::dumpXML() const {}
1026void Decl::dumpXML(llvm::raw_ostream &out) const {}
1027
1028#endif