1 //===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Decl::dump method, which pretty print the
11 // AST back out to C/Objective-C/C++/Objective-C++ code.
13 //===----------------------------------------------------------------------===//
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/DeclVisitor.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/AST/ExprCXX.h"
21 #include "clang/AST/PrettyPrinter.h"
22 #include "clang/Basic/Module.h"
23 #include "llvm/Support/raw_ostream.h"
24 using namespace clang;
27 class DeclPrinter : public DeclVisitor<DeclPrinter> {
30 PrintingPolicy Policy;
32 bool PrintInstantiation;
34 raw_ostream& Indent() { return Indent(Indentation); }
35 raw_ostream& Indent(unsigned Indentation);
36 void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls);
38 void Print(AccessSpecifier AS);
41 DeclPrinter(raw_ostream &Out, ASTContext &Context,
42 const PrintingPolicy &Policy,
43 unsigned Indentation = 0,
44 bool PrintInstantiation = false)
45 : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation),
46 PrintInstantiation(PrintInstantiation) { }
48 void VisitDeclContext(DeclContext *DC, bool Indent = true);
50 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
51 void VisitTypedefDecl(TypedefDecl *D);
52 void VisitTypeAliasDecl(TypeAliasDecl *D);
53 void VisitEnumDecl(EnumDecl *D);
54 void VisitRecordDecl(RecordDecl *D);
55 void VisitEnumConstantDecl(EnumConstantDecl *D);
56 void VisitFunctionDecl(FunctionDecl *D);
57 void VisitFieldDecl(FieldDecl *D);
58 void VisitVarDecl(VarDecl *D);
59 void VisitLabelDecl(LabelDecl *D);
60 void VisitParmVarDecl(ParmVarDecl *D);
61 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
62 void VisitImportDecl(ImportDecl *D);
63 void VisitStaticAssertDecl(StaticAssertDecl *D);
64 void VisitNamespaceDecl(NamespaceDecl *D);
65 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
66 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
67 void VisitCXXRecordDecl(CXXRecordDecl *D);
68 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
69 void VisitTemplateDecl(const TemplateDecl *D);
70 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
71 void VisitClassTemplateDecl(ClassTemplateDecl *D);
72 void VisitObjCMethodDecl(ObjCMethodDecl *D);
73 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
74 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
75 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
76 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
77 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
78 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
79 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
80 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
81 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
82 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
83 void VisitUsingDecl(UsingDecl *D);
84 void VisitUsingShadowDecl(UsingShadowDecl *D);
86 void PrintTemplateParameters(const TemplateParameterList *Params,
87 const TemplateArgumentList *Args);
88 void prettyPrintAttributes(Decl *D);
92 void Decl::print(raw_ostream &Out, unsigned Indentation,
93 bool PrintInstantiation) const {
94 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
97 void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
98 unsigned Indentation, bool PrintInstantiation) const {
99 DeclPrinter Printer(Out, getASTContext(), Policy, Indentation, PrintInstantiation);
100 Printer.Visit(const_cast<Decl*>(this));
103 static QualType GetBaseType(QualType T) {
104 // FIXME: This should be on the Type class!
105 QualType BaseType = T;
106 while (!BaseType->isSpecifierType()) {
107 if (isa<TypedefType>(BaseType))
109 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
110 BaseType = PTy->getPointeeType();
111 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
112 BaseType = ATy->getElementType();
113 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
114 BaseType = FTy->getResultType();
115 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
116 BaseType = VTy->getElementType();
117 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
118 BaseType = RTy->getPointeeType();
120 llvm_unreachable("Unknown declarator!");
125 static QualType getDeclType(Decl* D) {
126 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
127 return TDD->getUnderlyingType();
128 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
129 return VD->getType();
133 void Decl::printGroup(Decl** Begin, unsigned NumDecls,
134 raw_ostream &Out, const PrintingPolicy &Policy,
135 unsigned Indentation) {
137 (*Begin)->print(Out, Policy, Indentation);
141 Decl** End = Begin + NumDecls;
142 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
146 PrintingPolicy SubPolicy(Policy);
147 if (TD && TD->isCompleteDefinition()) {
148 TD->print(Out, Policy, Indentation);
150 SubPolicy.SuppressTag = true;
154 for ( ; Begin != End; ++Begin) {
156 SubPolicy.SuppressSpecifiers = false;
159 if (!isFirst) Out << ", ";
160 SubPolicy.SuppressSpecifiers = true;
163 (*Begin)->print(Out, SubPolicy, Indentation);
167 void DeclContext::dumpDeclContext() const {
168 // Get the translation unit
169 const DeclContext *DC = this;
170 while (!DC->isTranslationUnit())
171 DC = DC->getParent();
173 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
174 DeclPrinter Printer(llvm::errs(), Ctx, Ctx.getPrintingPolicy(), 0);
175 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
178 void Decl::dump(raw_ostream &Out) const {
179 PrintingPolicy Policy = getASTContext().getPrintingPolicy();
181 print(Out, Policy, /*Indentation*/ 0, /*PrintInstantiation*/ true);
184 raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
185 for (unsigned i = 0; i != Indentation; ++i)
190 void DeclPrinter::prettyPrintAttributes(Decl *D) {
192 AttrVec &Attrs = D->getAttrs();
193 for (AttrVec::const_iterator i=Attrs.begin(), e=Attrs.end(); i!=e; ++i) {
195 A->printPretty(Out, Context);
200 void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
202 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
208 void DeclPrinter::Print(AccessSpecifier AS) {
210 case AS_none: llvm_unreachable("No access specifier!");
211 case AS_public: Out << "public"; break;
212 case AS_protected: Out << "protected"; break;
213 case AS_private: Out << "private"; break;
217 //----------------------------------------------------------------------------
218 // Common C declarations
219 //----------------------------------------------------------------------------
221 void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
223 Indentation += Policy.Indentation;
225 SmallVector<Decl*, 2> Decls;
226 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
229 // Don't print ObjCIvarDecls, as they are printed when visiting the
230 // containing ObjCInterfaceDecl.
231 if (isa<ObjCIvarDecl>(*D))
235 // Skip over implicit declarations in pretty-printing mode.
236 if (D->isImplicit()) continue;
237 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
238 // of __builtin_va_list or __[u]int128_t. There should be some other way
240 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) {
241 if (IdentifierInfo *II = ND->getIdentifier()) {
242 if (II->isStr("__builtin_va_list") ||
243 II->isStr("__int128_t") || II->isStr("__uint128_t"))
249 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
250 // forced to merge the declarations because there's no other way to
251 // refer to the struct in question. This limited merging is safe without
252 // a bunch of other checks because it only merges declarations directly
253 // referring to the tag, not typedefs.
255 // Check whether the current declaration should be grouped with a previous
257 QualType CurDeclType = getDeclType(*D);
258 if (!Decls.empty() && !CurDeclType.isNull()) {
259 QualType BaseType = GetBaseType(CurDeclType);
260 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
261 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
267 // If we have a merged group waiting to be handled, handle it now.
269 ProcessDeclGroup(Decls);
271 // If the current declaration is an unnamed tag type, save it
272 // so we can merge it with the subsequent declaration(s) using it.
273 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
278 if (isa<AccessSpecDecl>(*D)) {
279 Indentation -= Policy.Indentation;
281 Print(D->getAccess());
283 Indentation += Policy.Indentation;
290 // FIXME: Need to be able to tell the DeclPrinter when
291 const char *Terminator = 0;
292 if (isa<FunctionDecl>(*D) &&
293 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
295 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
297 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
298 isa<ObjCImplementationDecl>(*D) ||
299 isa<ObjCInterfaceDecl>(*D) ||
300 isa<ObjCProtocolDecl>(*D) ||
301 isa<ObjCCategoryImplDecl>(*D) ||
302 isa<ObjCCategoryDecl>(*D))
304 else if (isa<EnumConstantDecl>(*D)) {
305 DeclContext::decl_iterator Next = D;
318 ProcessDeclGroup(Decls);
321 Indentation -= Policy.Indentation;
324 void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
325 VisitDeclContext(D, false);
328 void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
329 if (!Policy.SuppressSpecifiers) {
332 if (D->isModulePrivate())
333 Out << "__module_private__ ";
335 D->getUnderlyingType().print(Out, Policy, D->getName());
336 prettyPrintAttributes(D);
339 void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
340 Out << "using " << *D << " = " << D->getUnderlyingType().getAsString(Policy);
343 void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
344 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
345 Out << "__module_private__ ";
348 if (D->isScopedUsingClassTag())
356 Out << " : " << D->getIntegerType().stream(Policy);
358 if (D->isCompleteDefinition()) {
363 prettyPrintAttributes(D);
366 void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
367 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
368 Out << "__module_private__ ";
369 Out << D->getKindName();
370 if (D->getIdentifier())
373 if (D->isCompleteDefinition()) {
380 void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
382 if (Expr *Init = D->getInitExpr()) {
384 Init->printPretty(Out, Context, 0, Policy, Indentation);
388 void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
389 if (!Policy.SuppressSpecifiers) {
390 switch (D->getStorageClassAsWritten()) {
392 case SC_Extern: Out << "extern "; break;
393 case SC_Static: Out << "static "; break;
394 case SC_PrivateExtern: Out << "__private_extern__ "; break;
395 case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal:
396 llvm_unreachable("invalid for functions");
399 if (D->isInlineSpecified()) Out << "inline ";
400 if (D->isVirtualAsWritten()) Out << "virtual ";
401 if (D->isModulePrivate()) Out << "__module_private__ ";
404 PrintingPolicy SubPolicy(Policy);
405 SubPolicy.SuppressSpecifiers = false;
406 std::string Proto = D->getNameInfo().getAsString();
408 QualType Ty = D->getType();
409 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
410 Proto = '(' + Proto + ')';
411 Ty = PT->getInnerType();
414 if (isa<FunctionType>(Ty)) {
415 const FunctionType *AFT = Ty->getAs<FunctionType>();
416 const FunctionProtoType *FT = 0;
417 if (D->hasWrittenPrototype())
418 FT = dyn_cast<FunctionProtoType>(AFT);
422 llvm::raw_string_ostream POut(Proto);
423 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
424 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
426 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
429 if (FT->isVariadic()) {
430 if (D->getNumParams()) POut << ", ";
433 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
434 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
437 Proto += D->getParamDecl(i)->getNameAsString();
446 if (FT->isVolatile())
447 Proto += " volatile";
448 if (FT->isRestrict())
449 Proto += " restrict";
452 if (FT && FT->hasDynamicExceptionSpec()) {
454 if (FT->getExceptionSpecType() == EST_MSAny)
457 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
461 Proto += FT->getExceptionType(I).getAsString(SubPolicy);;
464 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
465 Proto += " noexcept";
466 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
468 llvm::raw_string_ostream EOut(Proto);
469 FT->getNoexceptExpr()->printPretty(EOut, Context, 0, SubPolicy,
477 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
478 bool HasInitializerList = false;
479 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
480 E = CDecl->init_end();
482 CXXCtorInitializer * BMInitializer = (*B);
483 if (BMInitializer->isInClassMemberInitializer())
486 if (!HasInitializerList) {
490 HasInitializerList = true;
494 if (BMInitializer->isAnyMemberInitializer()) {
495 FieldDecl *FD = BMInitializer->getAnyMember();
498 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
502 if (!BMInitializer->getInit()) {
505 Expr *Init = BMInitializer->getInit();
506 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
507 Init = Tmp->getSubExpr();
509 Init = Init->IgnoreParens();
511 Expr *SimpleInit = 0;
513 unsigned NumArgs = 0;
514 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
515 Args = ParenList->getExprs();
516 NumArgs = ParenList->getNumExprs();
517 } else if (CXXConstructExpr *Construct
518 = dyn_cast<CXXConstructExpr>(Init)) {
519 Args = Construct->getArgs();
520 NumArgs = Construct->getNumArgs();
525 SimpleInit->printPretty(Out, Context, 0, Policy, Indentation);
527 for (unsigned I = 0; I != NumArgs; ++I) {
528 if (isa<CXXDefaultArgExpr>(Args[I]))
533 Args[I]->printPretty(Out, Context, 0, Policy, Indentation);
541 AFT->getResultType().print(Out, Policy, Proto);
543 Ty.print(Out, Policy, Proto);
546 prettyPrintAttributes(D);
550 else if (D->isDeletedAsWritten())
552 else if (D->doesThisDeclarationHaveABody()) {
553 if (!D->hasPrototype() && D->getNumParams()) {
554 // This is a K&R function definition, so we need to print the
557 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
558 Indentation += Policy.Indentation;
559 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
561 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
564 Indentation -= Policy.Indentation;
568 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
573 void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
574 if (!Policy.SuppressSpecifiers && D->isMutable())
576 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
577 Out << "__module_private__ ";
579 Out << D->getType().stream(Policy, D->getName());
581 if (D->isBitField()) {
583 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
586 Expr *Init = D->getInClassInitializer();
587 if (!Policy.SuppressInitializers && Init) {
588 if (D->getInClassInitStyle() == ICIS_ListInit)
592 Init->printPretty(Out, Context, 0, Policy, Indentation);
594 prettyPrintAttributes(D);
597 void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
602 void DeclPrinter::VisitVarDecl(VarDecl *D) {
603 StorageClass SCAsWritten = D->getStorageClassAsWritten();
604 if (!Policy.SuppressSpecifiers && SCAsWritten != SC_None)
605 Out << VarDecl::getStorageClassSpecifierString(SCAsWritten) << " ";
607 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
609 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
610 Out << "__module_private__ ";
612 QualType T = D->getType();
613 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D))
614 T = Parm->getOriginalType();
615 T.print(Out, Policy, D->getName());
616 Expr *Init = D->getInit();
617 if (!Policy.SuppressInitializers && Init) {
618 bool ImplicitInit = false;
619 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init))
620 ImplicitInit = D->getInitStyle() == VarDecl::CallInit &&
621 Construct->getNumArgs() == 0 && !Construct->isListInitialization();
623 if (D->getInitStyle() == VarDecl::CallInit)
625 else if (D->getInitStyle() == VarDecl::CInit) {
628 Init->printPretty(Out, Context, 0, Policy, Indentation);
629 if (D->getInitStyle() == VarDecl::CallInit)
633 prettyPrintAttributes(D);
636 void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
640 void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
642 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
646 void DeclPrinter::VisitImportDecl(ImportDecl *D) {
647 Out << "@__experimental_modules_import " << D->getImportedModule()->getFullModuleName()
651 void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
652 Out << "static_assert(";
653 D->getAssertExpr()->printPretty(Out, Context, 0, Policy, Indentation);
655 D->getMessage()->printPretty(Out, Context, 0, Policy, Indentation);
659 //----------------------------------------------------------------------------
661 //----------------------------------------------------------------------------
662 void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
665 Out << "namespace " << *D << " {\n";
670 void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
671 Out << "using namespace ";
672 if (D->getQualifier())
673 D->getQualifier()->print(Out, Policy);
674 Out << *D->getNominatedNamespaceAsWritten();
677 void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
678 Out << "namespace " << *D << " = ";
679 if (D->getQualifier())
680 D->getQualifier()->print(Out, Policy);
681 Out << *D->getAliasedNamespace();
684 void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
685 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
686 Out << "__module_private__ ";
687 Out << D->getKindName();
688 if (D->getIdentifier())
691 if (D->isCompleteDefinition()) {
692 // Print the base classes
693 if (D->getNumBases()) {
695 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
696 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
697 if (Base != D->bases_begin())
700 if (Base->isVirtual())
703 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
706 Out << " " << Base->getType().getAsString(Policy);
708 if (Base->isPackExpansion())
713 // Print the class definition
714 // FIXME: Doesn't print access specifiers, e.g., "public:"
721 void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
723 if (D->getLanguage() == LinkageSpecDecl::lang_c)
726 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
727 "unknown language in linkage specification");
731 Out << "extern \"" << l << "\" ";
732 if (D->hasBraces()) {
737 Visit(*D->decls_begin());
740 void DeclPrinter::PrintTemplateParameters(
741 const TemplateParameterList *Params, const TemplateArgumentList *Args = 0) {
743 assert(!Args || Params->size() == Args->size());
747 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
751 const Decl *Param = Params->getParam(i);
752 if (const TemplateTypeParmDecl *TTP =
753 dyn_cast<TemplateTypeParmDecl>(Param)) {
755 if (TTP->wasDeclaredWithTypename())
760 if (TTP->isParameterPack())
767 Args->get(i).print(Policy, Out);
768 } else if (TTP->hasDefaultArgument()) {
770 Out << TTP->getDefaultArgument().getAsString(Policy);
772 } else if (const NonTypeTemplateParmDecl *NTTP =
773 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
774 Out << NTTP->getType().getAsString(Policy);
776 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType()))
779 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
781 Out << Name->getName();
786 Args->get(i).print(Policy, Out);
787 } else if (NTTP->hasDefaultArgument()) {
789 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
792 } else if (const TemplateTemplateParmDecl *TTPD =
793 dyn_cast<TemplateTemplateParmDecl>(Param)) {
794 VisitTemplateDecl(TTPD);
795 // FIXME: print the default argument, if present.
802 void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
803 PrintTemplateParameters(D->getTemplateParameters());
805 if (const TemplateTemplateParmDecl *TTP =
806 dyn_cast<TemplateTemplateParmDecl>(D)) {
808 if (TTP->isParameterPack())
812 Visit(D->getTemplatedDecl());
816 void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
817 if (PrintInstantiation) {
818 TemplateParameterList *Params = D->getTemplateParameters();
819 for (FunctionTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
821 PrintTemplateParameters(Params, (*I)->getTemplateSpecializationArgs());
826 return VisitRedeclarableTemplateDecl(D);
829 void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
830 if (PrintInstantiation) {
831 TemplateParameterList *Params = D->getTemplateParameters();
832 for (ClassTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
834 PrintTemplateParameters(Params, &(*I)->getTemplateArgs());
840 return VisitRedeclarableTemplateDecl(D);
843 //----------------------------------------------------------------------------
844 // Objective-C declarations
845 //----------------------------------------------------------------------------
847 void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
848 if (OMD->isInstanceMethod())
852 if (!OMD->getResultType().isNull())
853 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
855 std::string name = OMD->getSelector().getAsString();
856 std::string::size_type pos, lastPos = 0;
857 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
858 E = OMD->param_end(); PI != E; ++PI) {
859 // FIXME: selector is missing here!
860 pos = name.find_first_of(':', lastPos);
861 Out << " " << name.substr(lastPos, pos - lastPos);
862 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << **PI;
866 if (OMD->param_begin() == OMD->param_end())
869 if (OMD->isVariadic())
872 if (OMD->getBody()) {
874 OMD->getBody()->printPretty(Out, Context, 0, Policy);
879 void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
880 std::string I = OID->getNameAsString();
881 ObjCInterfaceDecl *SID = OID->getSuperClass();
884 Out << "@implementation " << I << " : " << *SID;
886 Out << "@implementation " << I;
888 VisitDeclContext(OID, false);
892 void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
893 std::string I = OID->getNameAsString();
894 ObjCInterfaceDecl *SID = OID->getSuperClass();
896 if (!OID->isThisDeclarationADefinition()) {
897 Out << "@class " << I << ";";
902 Out << "@interface " << I << " : " << *SID;
904 Out << "@interface " << I;
907 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
908 if (!Protocols.empty()) {
909 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
910 E = Protocols.end(); I != E; ++I)
911 Out << (I == Protocols.begin() ? '<' : ',') << **I;
914 if (!Protocols.empty())
917 if (OID->ivar_size() > 0) {
919 Indentation += Policy.Indentation;
920 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
921 E = OID->ivar_end(); I != E; ++I) {
922 Indent() << I->getType().getAsString(Policy) << ' ' << **I << ";\n";
924 Indentation -= Policy.Indentation;
928 VisitDeclContext(OID, false);
930 // FIXME: implement the rest...
933 void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
934 if (!PID->isThisDeclarationADefinition()) {
935 Out << "@protocol " << PID->getIdentifier() << ";\n";
939 Out << "@protocol " << *PID << '\n';
940 VisitDeclContext(PID, false);
944 void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
945 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
947 VisitDeclContext(PID, false);
949 // FIXME: implement the rest...
952 void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
953 Out << "@interface " << *PID->getClassInterface() << '(' << *PID << ")\n";
954 VisitDeclContext(PID, false);
957 // FIXME: implement the rest...
960 void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
961 Out << "@compatibility_alias " << *AID
962 << ' ' << *AID->getClassInterface() << ";\n";
965 /// PrintObjCPropertyDecl - print a property declaration.
967 void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
968 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
969 Out << "@required\n";
970 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
971 Out << "@optional\n";
974 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
977 if (PDecl->getPropertyAttributes() &
978 ObjCPropertyDecl::OBJC_PR_readonly) {
979 Out << (first ? ' ' : ',') << "readonly";
983 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
984 Out << (first ? ' ' : ',') << "getter = "
985 << PDecl->getGetterName().getAsString();
988 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
989 Out << (first ? ' ' : ',') << "setter = "
990 << PDecl->getSetterName().getAsString();
994 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
995 Out << (first ? ' ' : ',') << "assign";
999 if (PDecl->getPropertyAttributes() &
1000 ObjCPropertyDecl::OBJC_PR_readwrite) {
1001 Out << (first ? ' ' : ',') << "readwrite";
1005 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1006 Out << (first ? ' ' : ',') << "retain";
1010 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1011 Out << (first ? ' ' : ',') << "strong";
1015 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1016 Out << (first ? ' ' : ',') << "copy";
1020 if (PDecl->getPropertyAttributes() &
1021 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1022 Out << (first ? ' ' : ',') << "nonatomic";
1025 if (PDecl->getPropertyAttributes() &
1026 ObjCPropertyDecl::OBJC_PR_atomic) {
1027 Out << (first ? ' ' : ',') << "atomic";
1031 (void) first; // Silence dead store warning due to idiomatic code.
1034 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << *PDecl;
1037 void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1038 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1039 Out << "@synthesize ";
1042 Out << *PID->getPropertyDecl();
1043 if (PID->getPropertyIvarDecl())
1044 Out << '=' << *PID->getPropertyIvarDecl();
1047 void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
1049 D->getQualifier()->print(Out, Policy);
1054 DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1055 Out << "using typename ";
1056 D->getQualifier()->print(Out, Policy);
1057 Out << D->getDeclName();
1060 void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1062 D->getQualifier()->print(Out, Policy);
1063 Out << D->getDeclName();
1066 void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {