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> {
29 PrintingPolicy Policy;
31 bool PrintInstantiation;
33 raw_ostream& Indent() { return Indent(Indentation); }
34 raw_ostream& Indent(unsigned Indentation);
35 void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls);
37 void Print(AccessSpecifier AS);
40 DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy,
41 unsigned Indentation = 0, bool PrintInstantiation = false)
42 : Out(Out), Policy(Policy), Indentation(Indentation),
43 PrintInstantiation(PrintInstantiation) { }
45 void VisitDeclContext(DeclContext *DC, bool Indent = true);
47 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
48 void VisitTypedefDecl(TypedefDecl *D);
49 void VisitTypeAliasDecl(TypeAliasDecl *D);
50 void VisitEnumDecl(EnumDecl *D);
51 void VisitRecordDecl(RecordDecl *D);
52 void VisitEnumConstantDecl(EnumConstantDecl *D);
53 void VisitFunctionDecl(FunctionDecl *D);
54 void VisitFieldDecl(FieldDecl *D);
55 void VisitVarDecl(VarDecl *D);
56 void VisitLabelDecl(LabelDecl *D);
57 void VisitParmVarDecl(ParmVarDecl *D);
58 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
59 void VisitImportDecl(ImportDecl *D);
60 void VisitStaticAssertDecl(StaticAssertDecl *D);
61 void VisitNamespaceDecl(NamespaceDecl *D);
62 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
63 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
64 void VisitCXXRecordDecl(CXXRecordDecl *D);
65 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
66 void VisitTemplateDecl(const TemplateDecl *D);
67 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
68 void VisitClassTemplateDecl(ClassTemplateDecl *D);
69 void VisitObjCMethodDecl(ObjCMethodDecl *D);
70 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
71 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
72 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
73 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
74 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
75 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
76 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
77 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
78 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
79 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
80 void VisitUsingDecl(UsingDecl *D);
81 void VisitUsingShadowDecl(UsingShadowDecl *D);
83 void PrintTemplateParameters(const TemplateParameterList *Params,
84 const TemplateArgumentList *Args);
85 void prettyPrintAttributes(Decl *D);
89 void Decl::print(raw_ostream &Out, unsigned Indentation,
90 bool PrintInstantiation) const {
91 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
94 void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
95 unsigned Indentation, bool PrintInstantiation) const {
96 DeclPrinter Printer(Out, Policy, Indentation, PrintInstantiation);
97 Printer.Visit(const_cast<Decl*>(this));
100 static QualType GetBaseType(QualType T) {
101 // FIXME: This should be on the Type class!
102 QualType BaseType = T;
103 while (!BaseType->isSpecifierType()) {
104 if (isa<TypedefType>(BaseType))
106 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
107 BaseType = PTy->getPointeeType();
108 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
109 BaseType = BPy->getPointeeType();
110 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
111 BaseType = ATy->getElementType();
112 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
113 BaseType = FTy->getResultType();
114 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
115 BaseType = VTy->getElementType();
116 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
117 BaseType = RTy->getPointeeType();
119 llvm_unreachable("Unknown declarator!");
124 static QualType getDeclType(Decl* D) {
125 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
126 return TDD->getUnderlyingType();
127 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
128 return VD->getType();
132 void Decl::printGroup(Decl** Begin, unsigned NumDecls,
133 raw_ostream &Out, const PrintingPolicy &Policy,
134 unsigned Indentation) {
136 (*Begin)->print(Out, Policy, Indentation);
140 Decl** End = Begin + NumDecls;
141 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
145 PrintingPolicy SubPolicy(Policy);
146 if (TD && TD->isCompleteDefinition()) {
147 TD->print(Out, Policy, Indentation);
149 SubPolicy.SuppressTag = true;
153 for ( ; Begin != End; ++Begin) {
155 SubPolicy.SuppressSpecifiers = false;
158 if (!isFirst) Out << ", ";
159 SubPolicy.SuppressSpecifiers = true;
162 (*Begin)->print(Out, SubPolicy, Indentation);
166 void DeclContext::dumpDeclContext() const {
167 // Get the translation unit
168 const DeclContext *DC = this;
169 while (!DC->isTranslationUnit())
170 DC = DC->getParent();
172 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
173 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), 0);
174 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
177 void Decl::dump() const {
181 void Decl::dump(raw_ostream &Out) const {
182 PrintingPolicy Policy = getASTContext().getPrintingPolicy();
183 Policy.DumpSourceManager = &getASTContext().getSourceManager();
184 print(Out, Policy, /*Indentation*/ 0, /*PrintInstantiation*/ true);
187 raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
188 for (unsigned i = 0; i != Indentation; ++i)
193 void DeclPrinter::prettyPrintAttributes(Decl *D) {
194 if (Policy.SuppressAttributes)
198 AttrVec &Attrs = D->getAttrs();
199 for (AttrVec::const_iterator i=Attrs.begin(), e=Attrs.end(); i!=e; ++i) {
201 A->printPretty(Out, Policy);
206 void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
208 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
214 void DeclPrinter::Print(AccessSpecifier AS) {
216 case AS_none: llvm_unreachable("No access specifier!");
217 case AS_public: Out << "public"; break;
218 case AS_protected: Out << "protected"; break;
219 case AS_private: Out << "private"; break;
223 //----------------------------------------------------------------------------
224 // Common C declarations
225 //----------------------------------------------------------------------------
227 void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
228 if (Policy.TerseOutput)
232 Indentation += Policy.Indentation;
234 SmallVector<Decl*, 2> Decls;
235 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
238 // Don't print ObjCIvarDecls, as they are printed when visiting the
239 // containing ObjCInterfaceDecl.
240 if (isa<ObjCIvarDecl>(*D))
243 if (!Policy.DumpSourceManager) {
244 // Skip over implicit declarations in pretty-printing mode.
245 if (D->isImplicit()) continue;
246 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
247 // of __builtin_va_list or __[u]int128_t. There should be some other way
249 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) {
250 if (IdentifierInfo *II = ND->getIdentifier()) {
251 if (II->isStr("__builtin_va_list") ||
252 II->isStr("__int128_t") || II->isStr("__uint128_t"))
258 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
259 // forced to merge the declarations because there's no other way to
260 // refer to the struct in question. This limited merging is safe without
261 // a bunch of other checks because it only merges declarations directly
262 // referring to the tag, not typedefs.
264 // Check whether the current declaration should be grouped with a previous
266 QualType CurDeclType = getDeclType(*D);
267 if (!Decls.empty() && !CurDeclType.isNull()) {
268 QualType BaseType = GetBaseType(CurDeclType);
269 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
270 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
276 // If we have a merged group waiting to be handled, handle it now.
278 ProcessDeclGroup(Decls);
280 // If the current declaration is an unnamed tag type, save it
281 // so we can merge it with the subsequent declaration(s) using it.
282 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
287 if (isa<AccessSpecDecl>(*D)) {
288 Indentation -= Policy.Indentation;
290 Print(D->getAccess());
292 Indentation += Policy.Indentation;
299 // FIXME: Need to be able to tell the DeclPrinter when
300 const char *Terminator = 0;
301 if (isa<FunctionDecl>(*D) &&
302 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
304 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
306 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
307 isa<ObjCImplementationDecl>(*D) ||
308 isa<ObjCInterfaceDecl>(*D) ||
309 isa<ObjCProtocolDecl>(*D) ||
310 isa<ObjCCategoryImplDecl>(*D) ||
311 isa<ObjCCategoryDecl>(*D))
313 else if (isa<EnumConstantDecl>(*D)) {
314 DeclContext::decl_iterator Next = D;
327 ProcessDeclGroup(Decls);
330 Indentation -= Policy.Indentation;
333 void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
334 VisitDeclContext(D, false);
337 void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
338 if (!Policy.SuppressSpecifiers) {
341 if (D->isModulePrivate())
342 Out << "__module_private__ ";
344 D->getUnderlyingType().print(Out, Policy, D->getName());
345 prettyPrintAttributes(D);
348 void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
349 Out << "using " << *D << " = " << D->getUnderlyingType().getAsString(Policy);
352 void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
353 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
354 Out << "__module_private__ ";
357 if (D->isScopedUsingClassTag())
365 Out << " : " << D->getIntegerType().stream(Policy);
367 if (D->isCompleteDefinition()) {
372 prettyPrintAttributes(D);
375 void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
376 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
377 Out << "__module_private__ ";
378 Out << D->getKindName();
379 if (D->getIdentifier())
382 if (D->isCompleteDefinition()) {
389 void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
391 if (Expr *Init = D->getInitExpr()) {
393 Init->printPretty(Out, 0, Policy, Indentation);
397 void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
398 if (!Policy.SuppressSpecifiers) {
399 switch (D->getStorageClassAsWritten()) {
401 case SC_Extern: Out << "extern "; break;
402 case SC_Static: Out << "static "; break;
403 case SC_PrivateExtern: Out << "__private_extern__ "; break;
404 case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal:
405 llvm_unreachable("invalid for functions");
408 if (D->isInlineSpecified()) Out << "inline ";
409 if (D->isVirtualAsWritten()) Out << "virtual ";
410 if (D->isModulePrivate()) Out << "__module_private__ ";
413 PrintingPolicy SubPolicy(Policy);
414 SubPolicy.SuppressSpecifiers = false;
415 std::string Proto = D->getNameInfo().getAsString();
417 QualType Ty = D->getType();
418 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
419 Proto = '(' + Proto + ')';
420 Ty = PT->getInnerType();
423 if (isa<FunctionType>(Ty)) {
424 const FunctionType *AFT = Ty->getAs<FunctionType>();
425 const FunctionProtoType *FT = 0;
426 if (D->hasWrittenPrototype())
427 FT = dyn_cast<FunctionProtoType>(AFT);
431 llvm::raw_string_ostream POut(Proto);
432 DeclPrinter ParamPrinter(POut, SubPolicy, Indentation);
433 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
435 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
438 if (FT->isVariadic()) {
439 if (D->getNumParams()) POut << ", ";
442 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
443 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
446 Proto += D->getParamDecl(i)->getNameAsString();
455 if (FT->isVolatile())
456 Proto += " volatile";
457 if (FT->isRestrict())
458 Proto += " restrict";
461 if (FT && FT->hasDynamicExceptionSpec()) {
463 if (FT->getExceptionSpecType() == EST_MSAny)
466 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
470 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
473 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
474 Proto += " noexcept";
475 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
477 llvm::raw_string_ostream EOut(Proto);
478 FT->getNoexceptExpr()->printPretty(EOut, 0, SubPolicy,
486 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
487 bool HasInitializerList = false;
488 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
489 E = CDecl->init_end();
491 CXXCtorInitializer * BMInitializer = (*B);
492 if (BMInitializer->isInClassMemberInitializer())
495 if (!HasInitializerList) {
499 HasInitializerList = true;
503 if (BMInitializer->isAnyMemberInitializer()) {
504 FieldDecl *FD = BMInitializer->getAnyMember();
507 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
511 if (!BMInitializer->getInit()) {
514 Expr *Init = BMInitializer->getInit();
515 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
516 Init = Tmp->getSubExpr();
518 Init = Init->IgnoreParens();
520 Expr *SimpleInit = 0;
522 unsigned NumArgs = 0;
523 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
524 Args = ParenList->getExprs();
525 NumArgs = ParenList->getNumExprs();
526 } else if (CXXConstructExpr *Construct
527 = dyn_cast<CXXConstructExpr>(Init)) {
528 Args = Construct->getArgs();
529 NumArgs = Construct->getNumArgs();
534 SimpleInit->printPretty(Out, 0, Policy, Indentation);
536 for (unsigned I = 0; I != NumArgs; ++I) {
537 if (isa<CXXDefaultArgExpr>(Args[I]))
542 Args[I]->printPretty(Out, 0, Policy, Indentation);
550 AFT->getResultType().print(Out, Policy, Proto);
552 Ty.print(Out, Policy, Proto);
555 prettyPrintAttributes(D);
559 else if (D->isDeletedAsWritten())
561 else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) {
562 if (!D->hasPrototype() && D->getNumParams()) {
563 // This is a K&R function definition, so we need to print the
566 DeclPrinter ParamPrinter(Out, SubPolicy, Indentation);
567 Indentation += Policy.Indentation;
568 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
570 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
573 Indentation -= Policy.Indentation;
577 D->getBody()->printPretty(Out, 0, SubPolicy, Indentation);
582 void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
583 if (!Policy.SuppressSpecifiers && D->isMutable())
585 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
586 Out << "__module_private__ ";
588 Out << D->getType().stream(Policy, D->getName());
590 if (D->isBitField()) {
592 D->getBitWidth()->printPretty(Out, 0, Policy, Indentation);
595 Expr *Init = D->getInClassInitializer();
596 if (!Policy.SuppressInitializers && Init) {
597 if (D->getInClassInitStyle() == ICIS_ListInit)
601 Init->printPretty(Out, 0, Policy, Indentation);
603 prettyPrintAttributes(D);
606 void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
611 void DeclPrinter::VisitVarDecl(VarDecl *D) {
612 StorageClass SCAsWritten = D->getStorageClassAsWritten();
613 if (!Policy.SuppressSpecifiers && SCAsWritten != SC_None)
614 Out << VarDecl::getStorageClassSpecifierString(SCAsWritten) << " ";
616 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
618 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
619 Out << "__module_private__ ";
621 QualType T = D->getType();
622 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D))
623 T = Parm->getOriginalType();
624 T.print(Out, Policy, D->getName());
625 Expr *Init = D->getInit();
626 if (!Policy.SuppressInitializers && Init) {
627 bool ImplicitInit = false;
628 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init))
629 ImplicitInit = D->getInitStyle() == VarDecl::CallInit &&
630 Construct->getNumArgs() == 0 && !Construct->isListInitialization();
632 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
634 else if (D->getInitStyle() == VarDecl::CInit) {
637 Init->printPretty(Out, 0, Policy, Indentation);
638 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
642 prettyPrintAttributes(D);
645 void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
649 void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
651 D->getAsmString()->printPretty(Out, 0, Policy, Indentation);
655 void DeclPrinter::VisitImportDecl(ImportDecl *D) {
656 Out << "@__experimental_modules_import " << D->getImportedModule()->getFullModuleName()
660 void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
661 Out << "static_assert(";
662 D->getAssertExpr()->printPretty(Out, 0, Policy, Indentation);
664 D->getMessage()->printPretty(Out, 0, Policy, Indentation);
668 //----------------------------------------------------------------------------
670 //----------------------------------------------------------------------------
671 void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
674 Out << "namespace " << *D << " {\n";
679 void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
680 Out << "using namespace ";
681 if (D->getQualifier())
682 D->getQualifier()->print(Out, Policy);
683 Out << *D->getNominatedNamespaceAsWritten();
686 void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
687 Out << "namespace " << *D << " = ";
688 if (D->getQualifier())
689 D->getQualifier()->print(Out, Policy);
690 Out << *D->getAliasedNamespace();
693 void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
694 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
695 Out << "__module_private__ ";
696 Out << D->getKindName();
697 if (D->getIdentifier())
700 if (D->isCompleteDefinition()) {
701 // Print the base classes
702 if (D->getNumBases()) {
704 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
705 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
706 if (Base != D->bases_begin())
709 if (Base->isVirtual())
712 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
715 Out << " " << Base->getType().getAsString(Policy);
717 if (Base->isPackExpansion())
722 // Print the class definition
723 // FIXME: Doesn't print access specifiers, e.g., "public:"
730 void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
732 if (D->getLanguage() == LinkageSpecDecl::lang_c)
735 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
736 "unknown language in linkage specification");
740 Out << "extern \"" << l << "\" ";
741 if (D->hasBraces()) {
746 Visit(*D->decls_begin());
749 void DeclPrinter::PrintTemplateParameters(
750 const TemplateParameterList *Params, const TemplateArgumentList *Args = 0) {
752 assert(!Args || Params->size() == Args->size());
756 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
760 const Decl *Param = Params->getParam(i);
761 if (const TemplateTypeParmDecl *TTP =
762 dyn_cast<TemplateTypeParmDecl>(Param)) {
764 if (TTP->wasDeclaredWithTypename())
769 if (TTP->isParameterPack())
776 Args->get(i).print(Policy, Out);
777 } else if (TTP->hasDefaultArgument()) {
779 Out << TTP->getDefaultArgument().getAsString(Policy);
781 } else if (const NonTypeTemplateParmDecl *NTTP =
782 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
783 Out << NTTP->getType().getAsString(Policy);
785 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType()))
788 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
790 Out << Name->getName();
795 Args->get(i).print(Policy, Out);
796 } else if (NTTP->hasDefaultArgument()) {
798 NTTP->getDefaultArgument()->printPretty(Out, 0, Policy, Indentation);
800 } else if (const TemplateTemplateParmDecl *TTPD =
801 dyn_cast<TemplateTemplateParmDecl>(Param)) {
802 VisitTemplateDecl(TTPD);
803 // FIXME: print the default argument, if present.
810 void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
811 PrintTemplateParameters(D->getTemplateParameters());
813 if (const TemplateTemplateParmDecl *TTP =
814 dyn_cast<TemplateTemplateParmDecl>(D)) {
816 if (TTP->isParameterPack())
820 Visit(D->getTemplatedDecl());
824 void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
825 if (PrintInstantiation) {
826 TemplateParameterList *Params = D->getTemplateParameters();
827 for (FunctionTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
829 PrintTemplateParameters(Params, (*I)->getTemplateSpecializationArgs());
834 return VisitRedeclarableTemplateDecl(D);
837 void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
838 if (PrintInstantiation) {
839 TemplateParameterList *Params = D->getTemplateParameters();
840 for (ClassTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
842 PrintTemplateParameters(Params, &(*I)->getTemplateArgs());
848 return VisitRedeclarableTemplateDecl(D);
851 //----------------------------------------------------------------------------
852 // Objective-C declarations
853 //----------------------------------------------------------------------------
855 void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
856 if (OMD->isInstanceMethod())
860 if (!OMD->getResultType().isNull())
861 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
863 std::string name = OMD->getSelector().getAsString();
864 std::string::size_type pos, lastPos = 0;
865 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
866 E = OMD->param_end(); PI != E; ++PI) {
867 // FIXME: selector is missing here!
868 pos = name.find_first_of(':', lastPos);
869 Out << " " << name.substr(lastPos, pos - lastPos);
870 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << **PI;
874 if (OMD->param_begin() == OMD->param_end())
877 if (OMD->isVariadic())
880 if (OMD->getBody() && !Policy.TerseOutput) {
882 OMD->getBody()->printPretty(Out, 0, Policy);
887 void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
888 std::string I = OID->getNameAsString();
889 ObjCInterfaceDecl *SID = OID->getSuperClass();
892 Out << "@implementation " << I << " : " << *SID;
894 Out << "@implementation " << I;
896 VisitDeclContext(OID, false);
900 void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
901 std::string I = OID->getNameAsString();
902 ObjCInterfaceDecl *SID = OID->getSuperClass();
904 if (!OID->isThisDeclarationADefinition()) {
905 Out << "@class " << I << ";";
910 Out << "@interface " << I << " : " << *SID;
912 Out << "@interface " << I;
915 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
916 if (!Protocols.empty()) {
917 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
918 E = Protocols.end(); I != E; ++I)
919 Out << (I == Protocols.begin() ? '<' : ',') << **I;
922 if (!Protocols.empty())
925 if (OID->ivar_size() > 0) {
927 Indentation += Policy.Indentation;
928 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
929 E = OID->ivar_end(); I != E; ++I) {
930 Indent() << I->getType().getAsString(Policy) << ' ' << **I << ";\n";
932 Indentation -= Policy.Indentation;
936 VisitDeclContext(OID, false);
938 // FIXME: implement the rest...
941 void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
942 if (!PID->isThisDeclarationADefinition()) {
943 Out << "@protocol " << PID->getIdentifier() << ";\n";
947 Out << "@protocol " << *PID << '\n';
948 VisitDeclContext(PID, false);
952 void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
953 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
955 VisitDeclContext(PID, false);
957 // FIXME: implement the rest...
960 void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
961 Out << "@interface " << *PID->getClassInterface() << '(' << *PID << ")\n";
962 VisitDeclContext(PID, false);
965 // FIXME: implement the rest...
968 void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
969 Out << "@compatibility_alias " << *AID
970 << ' ' << *AID->getClassInterface() << ";\n";
973 /// PrintObjCPropertyDecl - print a property declaration.
975 void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
976 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
977 Out << "@required\n";
978 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
979 Out << "@optional\n";
982 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
985 if (PDecl->getPropertyAttributes() &
986 ObjCPropertyDecl::OBJC_PR_readonly) {
987 Out << (first ? ' ' : ',') << "readonly";
991 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
992 Out << (first ? ' ' : ',') << "getter = "
993 << PDecl->getGetterName().getAsString();
996 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
997 Out << (first ? ' ' : ',') << "setter = "
998 << PDecl->getSetterName().getAsString();
1002 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1003 Out << (first ? ' ' : ',') << "assign";
1007 if (PDecl->getPropertyAttributes() &
1008 ObjCPropertyDecl::OBJC_PR_readwrite) {
1009 Out << (first ? ' ' : ',') << "readwrite";
1013 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1014 Out << (first ? ' ' : ',') << "retain";
1018 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1019 Out << (first ? ' ' : ',') << "strong";
1023 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1024 Out << (first ? ' ' : ',') << "copy";
1028 if (PDecl->getPropertyAttributes() &
1029 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1030 Out << (first ? ' ' : ',') << "nonatomic";
1033 if (PDecl->getPropertyAttributes() &
1034 ObjCPropertyDecl::OBJC_PR_atomic) {
1035 Out << (first ? ' ' : ',') << "atomic";
1039 (void) first; // Silence dead store warning due to idiomatic code.
1042 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << *PDecl;
1045 void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1046 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1047 Out << "@synthesize ";
1050 Out << *PID->getPropertyDecl();
1051 if (PID->getPropertyIvarDecl())
1052 Out << '=' << *PID->getPropertyIvarDecl();
1055 void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
1057 D->getQualifier()->print(Out, Policy);
1062 DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1063 Out << "using typename ";
1064 D->getQualifier()->print(Out, Policy);
1065 Out << D->getDeclName();
1068 void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1070 D->getQualifier()->print(Out, Policy);
1071 Out << D->getDeclName();
1074 void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {