blob: 959bbcbb1748baf05315546f3ca3ca88d60617f6 [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 {
Chris Lattner5f9e2722011-07-23 10:55:15 +000056 StringRef Name;
John McCall26c25c92010-11-24 11:30:07 +000057 NodeState State;
Chris Lattner5f9e2722011-07-23 10:55:15 +000058 Node(StringRef name) : Name(name), State(NS_Attrs) {}
John McCall26c25c92010-11-24 11:30:07 +000059
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
Chris Lattner5f9e2722011-07-23 10:55:15 +0000162static StringRef getTypeKindName(Type *T) {
John McCall26c25c92010-11-24 11:30:07 +0000163 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> {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000175 raw_ostream &out;
John McCall26c25c92010-11-24 11:30:07 +0000176 ASTContext &Context;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000177 SmallVector<Node, 16> Stack;
John McCall26c25c92010-11-24 11:30:07 +0000178 unsigned Indent;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000179 explicit XMLDumper(raw_ostream &OS, ASTContext &context)
John McCall26c25c92010-11-24 11:30:07 +0000180 : 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.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000188 void push(StringRef name) {
John McCall26c25c92010-11-24 11:30:07 +0000189 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.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000203 void set(StringRef attr, StringRef value) {
John McCall26c25c92010-11-24 11:30:07 +0000204 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
Chris Lattner5f9e2722011-07-23 10:55:15 +0000229 void setPointer(StringRef prop, const void *p) {
John McCall26c25c92010-11-24 11:30:07 +0000230 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
Chris Lattner5f9e2722011-07-23 10:55:15 +0000241 void setInteger(StringRef prop, const llvm::APSInt &v) {
John McCall26c25c92010-11-24 11:30:07 +0000242 set(prop, v.toString(10));
243 }
244
Chris Lattner5f9e2722011-07-23 10:55:15 +0000245 void setInteger(StringRef prop, unsigned n) {
John McCall26c25c92010-11-24 11:30:07 +0000246 llvm::SmallString<10> buffer;
247 llvm::raw_svector_ostream os(buffer);
248 os << n;
249 os.flush();
250 set(prop, buffer);
251 }
252
Chris Lattner5f9e2722011-07-23 10:55:15 +0000253 void setFlag(StringRef prop, bool flag) {
John McCall26c25c92010-11-24 11:30:07 +0000254 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:
Chris Lattner5f9e2722011-07-23 10:55:15 +0000271 TemporaryContainer(XMLDumper &dumper, StringRef name)
John McCall26c25c92010-11-24 11:30:07 +0000272 : 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 }
Chris Lattner5f9e2722011-07-23 10:55:15 +0000306 void visitDeclRef(StringRef Name, Decl *D) {
John McCall3bddf5c2010-12-02 10:24:56 +0000307 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) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000426 StringRef lang = "";
John McCall26c25c92010-11-24 11:30:07 +0000427 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());
Sean Hunt10620eb2011-05-06 20:44:56 +0000485 setFlag("deleted", D->isDeletedAsWritten());
John McCall26c25c92010-11-24 11:30:07 +0000486 if (D->getStorageClass() != SC_None)
487 set("storage",
488 VarDecl::getStorageClassSpecifierString(D->getStorageClass()));
489 setFlag("inline", D->isInlineSpecified());
Joerg Sonnenbergercbea7632011-05-13 21:10:39 +0000490 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>())
491 set("asmlabel", ALA->getLabel());
John McCall26c25c92010-11-24 11:30:07 +0000492 // TODO: instantiation, etc.
493 }
494 void visitFunctionDeclChildren(FunctionDecl *D) {
495 for (FunctionDecl::param_iterator
496 I = D->param_begin(), E = D->param_end(); I != E; ++I)
497 dispatch(*I);
Sean Hunt10620eb2011-05-06 20:44:56 +0000498 if (D->doesThisDeclarationHaveABody())
John McCall26c25c92010-11-24 11:30:07 +0000499 dispatch(D->getBody());
500 }
501
502 // CXXMethodDecl ?
503 // CXXConstructorDecl ?
504 // CXXDestructorDecl ?
505 // CXXConversionDecl ?
506
Sean Huntcbb67482011-01-08 20:30:50 +0000507 void dispatch(CXXCtorInitializer *Init) {
John McCall3bddf5c2010-12-02 10:24:56 +0000508 // TODO
509 }
510
John McCall26c25c92010-11-24 11:30:07 +0000511 // FieldDecl
512 void visitFieldDeclAttrs(FieldDecl *D) {
513 setFlag("mutable", D->isMutable());
514 }
515 void visitFieldDeclChildren(FieldDecl *D) {
516 if (D->isBitField()) {
517 TemporaryContainer C(*this, "bitwidth");
518 dispatch(D->getBitWidth());
519 }
520 // TODO: C++0x member initializer
521 }
522
523 // EnumConstantDecl
524 void visitEnumConstantDeclChildren(EnumConstantDecl *D) {
525 // value in any case?
526 if (D->getInitExpr()) dispatch(D->getInitExpr());
527 }
528
529 // IndirectFieldDecl
530 void visitIndirectFieldDeclChildren(IndirectFieldDecl *D) {
531 for (IndirectFieldDecl::chain_iterator
532 I = D->chain_begin(), E = D->chain_end(); I != E; ++I) {
533 NamedDecl *VD = const_cast<NamedDecl*>(*I);
534 push(isa<VarDecl>(VD) ? "variable" : "field");
535 setPointer("ptr", VD);
536 completeAttrs();
537 pop();
538 }
539 }
540
541 // TypeDecl
542 void visitTypeDeclAttrs(TypeDecl *D) {
543 setPointer("typeptr", D->getTypeForDecl());
544 }
545
546 // TypedefDecl
547 void visitTypedefDeclAttrs(TypedefDecl *D) {
Richard Smith162e1c12011-04-15 14:24:37 +0000548 visitRedeclarableAttrs<TypedefNameDecl>(D);
John McCall26c25c92010-11-24 11:30:07 +0000549 }
550 void visitTypedefDeclChildren(TypedefDecl *D) {
551 dispatch(D->getTypeSourceInfo()->getTypeLoc());
552 }
553
Richard Smith162e1c12011-04-15 14:24:37 +0000554 // TypeAliasDecl
555 void visitTypeAliasDeclAttrs(TypeAliasDecl *D) {
556 visitRedeclarableAttrs<TypedefNameDecl>(D);
557 }
558 void visitTypeAliasDeclChildren(TypeAliasDecl *D) {
559 dispatch(D->getTypeSourceInfo()->getTypeLoc());
560 }
561
John McCall26c25c92010-11-24 11:30:07 +0000562 // TagDecl
563 void visitTagDeclAttrs(TagDecl *D) {
564 visitRedeclarableAttrs(D);
565 }
566 void visitTagDeclAsContext(TagDecl *D) {
567 visitDeclContext(D);
568 }
569
570 // EnumDecl
571 void visitEnumDeclAttrs(EnumDecl *D) {
572 setFlag("scoped", D->isScoped());
573 setFlag("fixed", D->isFixed());
574 }
575 void visitEnumDeclChildren(EnumDecl *D) {
576 {
577 TemporaryContainer C(*this, "promotion_type");
578 dispatch(D->getPromotionType());
579 }
580 {
581 TemporaryContainer C(*this, "integer_type");
582 dispatch(D->getIntegerType());
583 }
584 }
585
586 // RecordDecl ?
587
588 void visitCXXRecordDeclChildren(CXXRecordDecl *D) {
589 if (!D->isThisDeclarationADefinition()) return;
590
591 for (CXXRecordDecl::base_class_iterator
592 I = D->bases_begin(), E = D->bases_end(); I != E; ++I) {
593 push("base");
594 setAccess(I->getAccessSpecifier());
595 completeAttrs();
596 dispatch(I->getTypeSourceInfo()->getTypeLoc());
597 pop();
598 }
599 }
600
601 // ClassTemplateSpecializationDecl ?
602
603 // FileScopeAsmDecl ?
604
605 // BlockDecl
606 void visitBlockDeclAttrs(BlockDecl *D) {
607 setFlag("variadic", D->isVariadic());
608 }
609 void visitBlockDeclChildren(BlockDecl *D) {
610 for (FunctionDecl::param_iterator
611 I = D->param_begin(), E = D->param_end(); I != E; ++I)
612 dispatch(*I);
613 dispatch(D->getBody());
614 }
615
616 // AccessSpecDecl
617 void visitAccessSpecDeclAttrs(AccessSpecDecl *D) {
618 setAccess(D->getAccess());
619 }
620
621 // TemplateDecl
622 void visitTemplateDeclChildren(TemplateDecl *D) {
623 visitTemplateParameters(D->getTemplateParameters());
Richard Smith3e4c6c42011-05-05 21:57:07 +0000624 if (D->getTemplatedDecl())
625 dispatch(D->getTemplatedDecl());
John McCall26c25c92010-11-24 11:30:07 +0000626 }
627
628 // FunctionTemplateDecl
629 void visitFunctionTemplateDeclAttrs(FunctionTemplateDecl *D) {
630 visitRedeclarableAttrs(D);
631 }
632 void visitFunctionTemplateDeclChildren(FunctionTemplateDecl *D) {
633 // Mention all the specializations which don't have explicit
634 // declarations elsewhere.
635 for (FunctionTemplateDecl::spec_iterator
636 I = D->spec_begin(), E = D->spec_end(); I != E; ++I) {
637 FunctionTemplateSpecializationInfo *Info
638 = I->getTemplateSpecializationInfo();
639
640 bool Unknown = false;
641 switch (Info->getTemplateSpecializationKind()) {
642 case TSK_ImplicitInstantiation: Unknown = false; break;
643 case TSK_Undeclared: Unknown = true; break;
644
645 // These will be covered at their respective sites.
646 case TSK_ExplicitSpecialization: continue;
647 case TSK_ExplicitInstantiationDeclaration: continue;
648 case TSK_ExplicitInstantiationDefinition: continue;
649 }
650
651 TemporaryContainer C(*this,
652 Unknown ? "uninstantiated" : "instantiation");
653 visitTemplateArguments(*Info->TemplateArguments);
654 dispatch(Info->Function);
655 }
656 }
657
658 // ClasTemplateDecl
659 void visitClassTemplateDeclAttrs(ClassTemplateDecl *D) {
660 visitRedeclarableAttrs(D);
661 }
662 void visitClassTemplateDeclChildren(ClassTemplateDecl *D) {
663 // Mention all the specializations which don't have explicit
664 // declarations elsewhere.
665 for (ClassTemplateDecl::spec_iterator
666 I = D->spec_begin(), E = D->spec_end(); I != E; ++I) {
667
668 bool Unknown = false;
669 switch (I->getTemplateSpecializationKind()) {
670 case TSK_ImplicitInstantiation: Unknown = false; break;
671 case TSK_Undeclared: Unknown = true; break;
672
673 // These will be covered at their respective sites.
674 case TSK_ExplicitSpecialization: continue;
675 case TSK_ExplicitInstantiationDeclaration: continue;
676 case TSK_ExplicitInstantiationDefinition: continue;
677 }
678
679 TemporaryContainer C(*this,
680 Unknown ? "uninstantiated" : "instantiation");
681 visitTemplateArguments(I->getTemplateArgs());
682 dispatch(*I);
683 }
684 }
685
686 // TemplateTypeParmDecl
687 void visitTemplateTypeParmDeclAttrs(TemplateTypeParmDecl *D) {
688 setInteger("depth", D->getDepth());
689 setInteger("index", D->getIndex());
690 }
691 void visitTemplateTypeParmDeclChildren(TemplateTypeParmDecl *D) {
692 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
693 dispatch(D->getDefaultArgumentInfo()->getTypeLoc());
694 // parameter pack?
695 }
696
697 // NonTypeTemplateParmDecl
698 void visitNonTypeTemplateParmDeclAttrs(NonTypeTemplateParmDecl *D) {
699 setInteger("depth", D->getDepth());
700 setInteger("index", D->getIndex());
701 }
702 void visitNonTypeTemplateParmDeclChildren(NonTypeTemplateParmDecl *D) {
703 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
704 dispatch(D->getDefaultArgument());
705 // parameter pack?
706 }
707
708 // TemplateTemplateParmDecl
709 void visitTemplateTemplateParmDeclAttrs(TemplateTemplateParmDecl *D) {
710 setInteger("depth", D->getDepth());
711 setInteger("index", D->getIndex());
712 }
713 void visitTemplateTemplateParmDeclChildren(TemplateTemplateParmDecl *D) {
714 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
715 dispatch(D->getDefaultArgument());
716 // parameter pack?
717 }
718
719 // FriendDecl
720 void visitFriendDeclChildren(FriendDecl *D) {
721 if (TypeSourceInfo *T = D->getFriendType())
722 dispatch(T->getTypeLoc());
723 else
724 dispatch(D->getFriendDecl());
725 }
726
727 // UsingDirectiveDecl ?
728 // UsingDecl ?
729 // UsingShadowDecl ?
730 // NamespaceAliasDecl ?
731 // UnresolvedUsingValueDecl ?
732 // UnresolvedUsingTypenameDecl ?
733 // StaticAssertDecl ?
734
John McCall3bddf5c2010-12-02 10:24:56 +0000735 // ObjCImplDecl
736 void visitObjCImplDeclChildren(ObjCImplDecl *D) {
737 visitDeclRef(D->getClassInterface());
738 }
739 void visitObjCImplDeclAsContext(ObjCImplDecl *D) {
740 visitDeclContext(D);
741 }
742
John McCall3bddf5c2010-12-02 10:24:56 +0000743 // ObjCInterfaceDecl
744 void visitCategoryList(ObjCCategoryDecl *D) {
745 if (!D) return;
746
747 TemporaryContainer C(*this, "categories");
748 for (; D; D = D->getNextClassCategory())
749 visitDeclRef(D);
750 }
751 void visitObjCInterfaceDeclAttrs(ObjCInterfaceDecl *D) {
752 setPointer("typeptr", D->getTypeForDecl());
Douglas Gregor7723fec2011-12-15 20:29:51 +0000753 setFlag("forward_decl", !D->isThisDeclarationADefinition());
John McCall3bddf5c2010-12-02 10:24:56 +0000754 setFlag("implicit_interface", D->isImplicitInterfaceDecl());
755 }
756 void visitObjCInterfaceDeclChildren(ObjCInterfaceDecl *D) {
757 visitDeclRef("super", D->getSuperClass());
758 visitDeclRef("implementation", D->getImplementation());
759 if (D->protocol_begin() != D->protocol_end()) {
760 TemporaryContainer C(*this, "protocols");
761 for (ObjCInterfaceDecl::protocol_iterator
762 I = D->protocol_begin(), E = D->protocol_end(); I != E; ++I)
763 visitDeclRef(*I);
764 }
765 visitCategoryList(D->getCategoryList());
766 }
767 void visitObjCInterfaceDeclAsContext(ObjCInterfaceDecl *D) {
768 visitDeclContext(D);
769 }
770
771 // ObjCCategoryDecl
772 void visitObjCCategoryDeclAttrs(ObjCCategoryDecl *D) {
773 setFlag("extension", D->IsClassExtension());
774 setFlag("synth_bitfield", D->hasSynthBitfield());
775 }
776 void visitObjCCategoryDeclChildren(ObjCCategoryDecl *D) {
777 visitDeclRef("interface", D->getClassInterface());
778 visitDeclRef("implementation", D->getImplementation());
779 if (D->protocol_begin() != D->protocol_end()) {
780 TemporaryContainer C(*this, "protocols");
781 for (ObjCCategoryDecl::protocol_iterator
782 I = D->protocol_begin(), E = D->protocol_end(); I != E; ++I)
783 visitDeclRef(*I);
784 }
785 }
786 void visitObjCCategoryDeclAsContext(ObjCCategoryDecl *D) {
787 visitDeclContext(D);
788 }
789
790 // ObjCCategoryImplDecl
791 void visitObjCCategoryImplDeclAttrs(ObjCCategoryImplDecl *D) {
792 set("identifier", D->getName());
793 }
794 void visitObjCCategoryImplDeclChildren(ObjCCategoryImplDecl *D) {
795 visitDeclRef(D->getCategoryDecl());
796 }
797
798 // ObjCImplementationDecl
799 void visitObjCImplementationDeclAttrs(ObjCImplementationDecl *D) {
800 setFlag("synth_bitfield", D->hasSynthBitfield());
801 set("identifier", D->getName());
802 }
803 void visitObjCImplementationDeclChildren(ObjCImplementationDecl *D) {
804 visitDeclRef("super", D->getSuperClass());
805 if (D->init_begin() != D->init_end()) {
806 TemporaryContainer C(*this, "initializers");
807 for (ObjCImplementationDecl::init_iterator
808 I = D->init_begin(), E = D->init_end(); I != E; ++I)
809 dispatch(*I);
810 }
811 }
812
813 // ObjCForwardProtocolDecl
814 void visitObjCForwardProtocolDeclChildren(ObjCForwardProtocolDecl *D) {
815 for (ObjCForwardProtocolDecl::protocol_iterator
816 I = D->protocol_begin(), E = D->protocol_end(); I != E; ++I)
817 visitDeclRef(*I);
818 }
819
820 // ObjCProtocolDecl
821 void visitObjCProtocolDeclAttrs(ObjCProtocolDecl *D) {
822 setFlag("forward_decl", D->isForwardDecl());
823 }
824 void visitObjCProtocolDeclChildren(ObjCProtocolDecl *D) {
825 if (D->protocol_begin() != D->protocol_end()) {
826 TemporaryContainer C(*this, "protocols");
827 for (ObjCInterfaceDecl::protocol_iterator
828 I = D->protocol_begin(), E = D->protocol_end(); I != E; ++I)
829 visitDeclRef(*I);
830 }
831 }
832 void visitObjCProtocolDeclAsContext(ObjCProtocolDecl *D) {
833 visitDeclContext(D);
834 }
835
836 // ObjCMethodDecl
837 void visitObjCMethodDeclAttrs(ObjCMethodDecl *D) {
838 // decl qualifier?
839 // implementation control?
840
841 setFlag("instance", D->isInstanceMethod());
842 setFlag("variadic", D->isVariadic());
843 setFlag("synthesized", D->isSynthesized());
844 setFlag("defined", D->isDefined());
Douglas Gregor926df6c2011-06-11 01:09:30 +0000845 setFlag("related_result_type", D->hasRelatedResultType());
John McCall3bddf5c2010-12-02 10:24:56 +0000846 }
847 void visitObjCMethodDeclChildren(ObjCMethodDecl *D) {
848 dispatch(D->getResultType());
849 for (ObjCMethodDecl::param_iterator
850 I = D->param_begin(), E = D->param_end(); I != E; ++I)
851 dispatch(*I);
852 if (D->isThisDeclarationADefinition())
853 dispatch(D->getBody());
854 }
855
856 // ObjCIvarDecl
Chris Lattner5f9e2722011-07-23 10:55:15 +0000857 void setAccessControl(StringRef prop, ObjCIvarDecl::AccessControl AC) {
John McCall3bddf5c2010-12-02 10:24:56 +0000858 switch (AC) {
859 case ObjCIvarDecl::None: return set(prop, "none");
860 case ObjCIvarDecl::Private: return set(prop, "private");
861 case ObjCIvarDecl::Protected: return set(prop, "protected");
862 case ObjCIvarDecl::Public: return set(prop, "public");
863 case ObjCIvarDecl::Package: return set(prop, "package");
864 }
865 }
866 void visitObjCIvarDeclAttrs(ObjCIvarDecl *D) {
867 setFlag("synthesize", D->getSynthesize());
868 setAccessControl("access", D->getAccessControl());
869 }
870
871 // ObjCCompatibleAliasDecl
872 void visitObjCCompatibleAliasDeclChildren(ObjCCompatibleAliasDecl *D) {
873 visitDeclRef(D->getClassInterface());
874 }
875
876 // FIXME: ObjCPropertyDecl
877 // FIXME: ObjCPropertyImplDecl
878
John McCall26c25c92010-11-24 11:30:07 +0000879 //---- Types -----------------------------------------------------//
880 void dispatch(TypeLoc TL) {
881 dispatch(TL.getType()); // for now
882 }
883
884 void dispatch(QualType T) {
885 if (T.hasLocalQualifiers()) {
886 push("QualType");
887 Qualifiers Qs = T.getLocalQualifiers();
888 setFlag("const", Qs.hasConst());
889 setFlag("volatile", Qs.hasVolatile());
890 setFlag("restrict", Qs.hasRestrict());
891 if (Qs.hasAddressSpace()) setInteger("addrspace", Qs.getAddressSpace());
892 if (Qs.hasObjCGCAttr()) {
893 switch (Qs.getObjCGCAttr()) {
894 case Qualifiers::Weak: set("gc", "weak"); break;
895 case Qualifiers::Strong: set("gc", "strong"); break;
896 case Qualifiers::GCNone: llvm_unreachable("explicit none");
897 }
898 }
899
900 completeAttrs();
901 dispatch(QualType(T.getTypePtr(), 0));
902 pop();
903 return;
904 }
905
906 Type *Ty = const_cast<Type*>(T.getTypePtr());
907 push(getTypeKindName(Ty));
John McCall13cf5e22010-11-24 11:53:13 +0000908 XMLTypeVisitor<XMLDumper>::dispatch(const_cast<Type*>(T.getTypePtr()));
John McCall26c25c92010-11-24 11:30:07 +0000909 pop();
910 }
911
912 void setCallingConv(CallingConv CC) {
913 switch (CC) {
914 case CC_Default: return;
915 case CC_C: return set("cc", "cdecl");
916 case CC_X86FastCall: return set("cc", "x86_fastcall");
917 case CC_X86StdCall: return set("cc", "x86_stdcall");
918 case CC_X86ThisCall: return set("cc", "x86_thiscall");
919 case CC_X86Pascal: return set("cc", "x86_pascal");
Anton Korobeynikov414d8962011-04-14 20:06:49 +0000920 case CC_AAPCS: return set("cc", "aapcs");
921 case CC_AAPCS_VFP: return set("cc", "aapcs_vfp");
John McCall26c25c92010-11-24 11:30:07 +0000922 }
923 }
924
925 void visitTypeAttrs(Type *D) {
926 setPointer(D);
927 setFlag("dependent", D->isDependentType());
928 setFlag("variably_modified", D->isVariablyModifiedType());
929
930 setPointer("canonical", D->getCanonicalTypeInternal().getAsOpaquePtr());
931 }
932
933 void visitPointerTypeChildren(PointerType *T) {
934 dispatch(T->getPointeeType());
935 }
936 void visitReferenceTypeChildren(ReferenceType *T) {
937 dispatch(T->getPointeeType());
938 }
939 void visitObjCObjectPointerTypeChildren(ObjCObjectPointerType *T) {
940 dispatch(T->getPointeeType());
941 }
942 void visitBlockPointerTypeChildren(BlockPointerType *T) {
943 dispatch(T->getPointeeType());
944 }
945
946 // Types that just wrap declarations.
947 void visitTagTypeChildren(TagType *T) {
948 visitDeclRef(T->getDecl());
949 }
950 void visitTypedefTypeChildren(TypedefType *T) {
951 visitDeclRef(T->getDecl());
952 }
953 void visitObjCInterfaceTypeChildren(ObjCInterfaceType *T) {
954 visitDeclRef(T->getDecl());
955 }
956 void visitUnresolvedUsingTypeChildren(UnresolvedUsingType *T) {
957 visitDeclRef(T->getDecl());
958 }
959 void visitInjectedClassNameTypeChildren(InjectedClassNameType *T) {
960 visitDeclRef(T->getDecl());
961 }
962
963 void visitFunctionTypeAttrs(FunctionType *T) {
964 setFlag("noreturn", T->getNoReturnAttr());
965 setCallingConv(T->getCallConv());
Eli Friedmana49218e2011-04-09 08:18:08 +0000966 if (T->getHasRegParm()) setInteger("regparm", T->getRegParmType());
John McCall26c25c92010-11-24 11:30:07 +0000967 }
968 void visitFunctionTypeChildren(FunctionType *T) {
969 dispatch(T->getResultType());
970 }
971
972 void visitFunctionProtoTypeAttrs(FunctionProtoType *T) {
973 setFlag("const", T->getTypeQuals() & Qualifiers::Const);
974 setFlag("volatile", T->getTypeQuals() & Qualifiers::Volatile);
975 setFlag("restrict", T->getTypeQuals() & Qualifiers::Restrict);
976 }
977 void visitFunctionProtoTypeChildren(FunctionProtoType *T) {
978 push("parameters");
979 setFlag("variadic", T->isVariadic());
980 completeAttrs();
981 for (FunctionProtoType::arg_type_iterator
982 I = T->arg_type_begin(), E = T->arg_type_end(); I != E; ++I)
983 dispatch(*I);
984 pop();
985
Sebastian Redl60618fa2011-03-12 11:50:43 +0000986 if (T->hasDynamicExceptionSpec()) {
John McCall26c25c92010-11-24 11:30:07 +0000987 push("exception_specifiers");
Sebastian Redl60618fa2011-03-12 11:50:43 +0000988 setFlag("any", T->getExceptionSpecType() == EST_MSAny);
John McCall26c25c92010-11-24 11:30:07 +0000989 completeAttrs();
990 for (FunctionProtoType::exception_iterator
991 I = T->exception_begin(), E = T->exception_end(); I != E; ++I)
992 dispatch(*I);
993 pop();
994 }
Sebastian Redl60618fa2011-03-12 11:50:43 +0000995 // FIXME: noexcept specifier
John McCall26c25c92010-11-24 11:30:07 +0000996 }
997
998 void visitTemplateSpecializationTypeChildren(TemplateSpecializationType *T) {
999 if (const RecordType *RT = T->getAs<RecordType>())
1000 visitDeclRef(RT->getDecl());
1001
1002 // TODO: TemplateName
1003
1004 push("template_arguments");
1005 completeAttrs();
1006 for (unsigned I = 0, E = T->getNumArgs(); I != E; ++I)
1007 dispatch(T->getArg(I));
1008 pop();
1009 }
1010
1011 //---- Statements ------------------------------------------------//
1012 void dispatch(Stmt *S) {
1013 // FIXME: this is not really XML at all
1014 push("Stmt");
John McCall3bddf5c2010-12-02 10:24:56 +00001015 out << ">\n";
John McCall26c25c92010-11-24 11:30:07 +00001016 Stack.back().State = NS_Children; // explicitly become non-lazy
1017 S->dump(out, Context.getSourceManager());
1018 out << '\n';
1019 pop();
1020 }
1021};
1022}
1023
1024void Decl::dumpXML() const {
1025 dumpXML(llvm::errs());
1026}
1027
Chris Lattner5f9e2722011-07-23 10:55:15 +00001028void Decl::dumpXML(raw_ostream &out) const {
John McCall26c25c92010-11-24 11:30:07 +00001029 XMLDumper(out, getASTContext()).dispatch(const_cast<Decl*>(this));
1030}
1031
1032#else /* ifndef NDEBUG */
1033
1034void Decl::dumpXML() const {}
Chris Lattner5f9e2722011-07-23 10:55:15 +00001035void Decl::dumpXML(raw_ostream &out) const {}
John McCall26c25c92010-11-24 11:30:07 +00001036
1037#endif