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::print method, which pretty prints the
11 // AST back out to C/Objective-C/C++/Objective-C++ code.
13 //===----------------------------------------------------------------------===//
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/Attr.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/AST/DeclVisitor.h"
20 #include "clang/AST/Expr.h"
21 #include "clang/AST/ExprCXX.h"
22 #include "clang/AST/PrettyPrinter.h"
23 #include "clang/Basic/Module.h"
24 #include "llvm/Support/raw_ostream.h"
25 using namespace clang;
28 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, const PrintingPolicy &Policy,
42 unsigned Indentation = 0, bool PrintInstantiation = false)
43 : Out(Out), Policy(Policy), Indentation(Indentation),
44 PrintInstantiation(PrintInstantiation) { }
46 void VisitDeclContext(DeclContext *DC, bool Indent = true);
48 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
49 void VisitTypedefDecl(TypedefDecl *D);
50 void VisitTypeAliasDecl(TypeAliasDecl *D);
51 void VisitEnumDecl(EnumDecl *D);
52 void VisitRecordDecl(RecordDecl *D);
53 void VisitEnumConstantDecl(EnumConstantDecl *D);
54 void VisitEmptyDecl(EmptyDecl *D);
55 void VisitFunctionDecl(FunctionDecl *D);
56 void VisitFriendDecl(FriendDecl *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);
85 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
87 void PrintTemplateParameters(const TemplateParameterList *Params,
88 const TemplateArgumentList *Args = nullptr);
89 void prettyPrintAttributes(Decl *D);
93 void Decl::print(raw_ostream &Out, unsigned Indentation,
94 bool PrintInstantiation) const {
95 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
98 void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
99 unsigned Indentation, bool PrintInstantiation) const {
100 DeclPrinter Printer(Out, Policy, Indentation, PrintInstantiation);
101 Printer.Visit(const_cast<Decl*>(this));
104 static QualType GetBaseType(QualType T) {
105 // FIXME: This should be on the Type class!
106 QualType BaseType = T;
107 while (!BaseType->isSpecifierType()) {
108 if (isa<TypedefType>(BaseType))
110 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
111 BaseType = PTy->getPointeeType();
112 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
113 BaseType = BPy->getPointeeType();
114 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
115 BaseType = ATy->getElementType();
116 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
117 BaseType = FTy->getReturnType();
118 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
119 BaseType = VTy->getElementType();
120 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
121 BaseType = RTy->getPointeeType();
123 llvm_unreachable("Unknown declarator!");
128 static QualType getDeclType(Decl* D) {
129 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
130 return TDD->getUnderlyingType();
131 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
132 return VD->getType();
136 void Decl::printGroup(Decl** Begin, unsigned NumDecls,
137 raw_ostream &Out, const PrintingPolicy &Policy,
138 unsigned Indentation) {
140 (*Begin)->print(Out, Policy, Indentation);
144 Decl** End = Begin + NumDecls;
145 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
149 PrintingPolicy SubPolicy(Policy);
150 if (TD && TD->isCompleteDefinition()) {
151 TD->print(Out, Policy, Indentation);
153 SubPolicy.SuppressTag = true;
157 for ( ; Begin != End; ++Begin) {
159 SubPolicy.SuppressSpecifiers = false;
162 if (!isFirst) Out << ", ";
163 SubPolicy.SuppressSpecifiers = true;
166 (*Begin)->print(Out, SubPolicy, Indentation);
170 LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const {
171 // Get the translation unit
172 const DeclContext *DC = this;
173 while (!DC->isTranslationUnit())
174 DC = DC->getParent();
176 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
177 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), 0);
178 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
181 raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
182 for (unsigned i = 0; i != Indentation; ++i)
187 void DeclPrinter::prettyPrintAttributes(Decl *D) {
188 if (Policy.PolishForDeclaration)
192 AttrVec &Attrs = D->getAttrs();
193 for (AttrVec::const_iterator i=Attrs.begin(), e=Attrs.end(); i!=e; ++i) {
195 A->printPretty(Out, Policy);
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) {
222 if (Policy.TerseOutput)
226 Indentation += Policy.Indentation;
228 SmallVector<Decl*, 2> Decls;
229 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
232 // Don't print ObjCIvarDecls, as they are printed when visiting the
233 // containing ObjCInterfaceDecl.
234 if (isa<ObjCIvarDecl>(*D))
237 // Skip over implicit declarations in pretty-printing mode.
241 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
242 // forced to merge the declarations because there's no other way to
243 // refer to the struct in question. This limited merging is safe without
244 // a bunch of other checks because it only merges declarations directly
245 // referring to the tag, not typedefs.
247 // Check whether the current declaration should be grouped with a previous
249 QualType CurDeclType = getDeclType(*D);
250 if (!Decls.empty() && !CurDeclType.isNull()) {
251 QualType BaseType = GetBaseType(CurDeclType);
252 if (!BaseType.isNull() && isa<ElaboratedType>(BaseType))
253 BaseType = cast<ElaboratedType>(BaseType)->getNamedType();
254 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
255 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
261 // If we have a merged group waiting to be handled, handle it now.
263 ProcessDeclGroup(Decls);
265 // If the current declaration is an unnamed tag type, save it
266 // so we can merge it with the subsequent declaration(s) using it.
267 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
272 if (isa<AccessSpecDecl>(*D)) {
273 Indentation -= Policy.Indentation;
275 Print(D->getAccess());
277 Indentation += Policy.Indentation;
284 // FIXME: Need to be able to tell the DeclPrinter when
285 const char *Terminator = nullptr;
286 if (isa<OMPThreadPrivateDecl>(*D))
287 Terminator = nullptr;
288 else if (isa<FunctionDecl>(*D) &&
289 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
290 Terminator = nullptr;
291 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
292 Terminator = nullptr;
293 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
294 isa<ObjCImplementationDecl>(*D) ||
295 isa<ObjCInterfaceDecl>(*D) ||
296 isa<ObjCProtocolDecl>(*D) ||
297 isa<ObjCCategoryImplDecl>(*D) ||
298 isa<ObjCCategoryDecl>(*D))
299 Terminator = nullptr;
300 else if (isa<EnumConstantDecl>(*D)) {
301 DeclContext::decl_iterator Next = D;
314 ProcessDeclGroup(Decls);
317 Indentation -= Policy.Indentation;
320 void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
321 VisitDeclContext(D, false);
324 void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
325 if (!Policy.SuppressSpecifiers) {
328 if (D->isModulePrivate())
329 Out << "__module_private__ ";
331 D->getTypeSourceInfo()->getType().print(Out, Policy, D->getName());
332 prettyPrintAttributes(D);
335 void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
336 Out << "using " << *D;
337 prettyPrintAttributes(D);
338 Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
341 void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
342 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
343 Out << "__module_private__ ";
346 if (D->isScopedUsingClassTag())
354 Out << " : " << D->getIntegerType().stream(Policy);
356 if (D->isCompleteDefinition()) {
361 prettyPrintAttributes(D);
364 void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
365 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
366 Out << "__module_private__ ";
367 Out << D->getKindName();
368 if (D->getIdentifier())
371 if (D->isCompleteDefinition()) {
378 void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
380 if (Expr *Init = D->getInitExpr()) {
382 Init->printPretty(Out, nullptr, Policy, Indentation);
386 void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
387 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
388 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
389 if (!Policy.SuppressSpecifiers) {
390 switch (D->getStorageClass()) {
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__ ";
402 if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr ";
403 if ((CDecl && CDecl->isExplicitSpecified()) ||
404 (ConversionDecl && ConversionDecl->isExplicit()))
408 PrintingPolicy SubPolicy(Policy);
409 SubPolicy.SuppressSpecifiers = false;
410 std::string Proto = D->getNameInfo().getAsString();
412 QualType Ty = D->getType();
413 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
414 Proto = '(' + Proto + ')';
415 Ty = PT->getInnerType();
418 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
419 const FunctionProtoType *FT = nullptr;
420 if (D->hasWrittenPrototype())
421 FT = dyn_cast<FunctionProtoType>(AFT);
425 llvm::raw_string_ostream POut(Proto);
426 DeclPrinter ParamPrinter(POut, SubPolicy, Indentation);
427 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
429 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
432 if (FT->isVariadic()) {
433 if (D->getNumParams()) POut << ", ";
436 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
437 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
440 Proto += D->getParamDecl(i)->getNameAsString();
449 if (FT->isVolatile())
450 Proto += " volatile";
451 if (FT->isRestrict())
452 Proto += " restrict";
454 switch (FT->getRefQualifier()) {
466 if (FT && FT->hasDynamicExceptionSpec()) {
468 if (FT->getExceptionSpecType() == EST_MSAny)
471 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
475 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
478 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
479 Proto += " noexcept";
480 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
482 llvm::raw_string_ostream EOut(Proto);
483 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
492 bool HasInitializerList = false;
493 for (const auto *BMInitializer : CDecl->inits()) {
494 if (BMInitializer->isInClassMemberInitializer())
497 if (!HasInitializerList) {
501 HasInitializerList = true;
505 if (BMInitializer->isAnyMemberInitializer()) {
506 FieldDecl *FD = BMInitializer->getAnyMember();
509 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
513 if (!BMInitializer->getInit()) {
516 Expr *Init = BMInitializer->getInit();
517 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
518 Init = Tmp->getSubExpr();
520 Init = Init->IgnoreParens();
522 Expr *SimpleInit = nullptr;
523 Expr **Args = nullptr;
524 unsigned NumArgs = 0;
525 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
526 Args = ParenList->getExprs();
527 NumArgs = ParenList->getNumExprs();
528 } else if (CXXConstructExpr *Construct
529 = dyn_cast<CXXConstructExpr>(Init)) {
530 Args = Construct->getArgs();
531 NumArgs = Construct->getNumArgs();
536 SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
538 for (unsigned I = 0; I != NumArgs; ++I) {
539 assert(Args[I] != nullptr && "Expected non-null Expr");
540 if (isa<CXXDefaultArgExpr>(Args[I]))
545 Args[I]->printPretty(Out, nullptr, Policy, Indentation);
550 if (BMInitializer->isPackExpansion())
553 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
554 if (FT && FT->hasTrailingReturn()) {
555 Out << "auto " << Proto << " -> ";
558 AFT->getReturnType().print(Out, Policy, Proto);
563 Ty.print(Out, Policy, Proto);
566 prettyPrintAttributes(D);
570 else if (D->isDeletedAsWritten())
572 else if (D->isExplicitlyDefaulted())
574 else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) {
575 if (!D->hasPrototype() && D->getNumParams()) {
576 // This is a K&R function definition, so we need to print the
579 DeclPrinter ParamPrinter(Out, SubPolicy, Indentation);
580 Indentation += Policy.Indentation;
581 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
583 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
586 Indentation -= Policy.Indentation;
591 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
596 void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
597 if (TypeSourceInfo *TSI = D->getFriendType()) {
598 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
599 for (unsigned i = 0; i < NumTPLists; ++i)
600 PrintTemplateParameters(D->getFriendTypeTemplateParameterList(i));
602 Out << " " << TSI->getType().getAsString(Policy);
604 else if (FunctionDecl *FD =
605 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
607 VisitFunctionDecl(FD);
609 else if (FunctionTemplateDecl *FTD =
610 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
612 VisitFunctionTemplateDecl(FTD);
614 else if (ClassTemplateDecl *CTD =
615 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
617 VisitRedeclarableTemplateDecl(CTD);
621 void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
622 if (!Policy.SuppressSpecifiers && D->isMutable())
624 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
625 Out << "__module_private__ ";
627 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
628 stream(Policy, D->getName());
630 if (D->isBitField()) {
632 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
635 Expr *Init = D->getInClassInitializer();
636 if (!Policy.SuppressInitializers && Init) {
637 if (D->getInClassInitStyle() == ICIS_ListInit)
641 Init->printPretty(Out, nullptr, Policy, Indentation);
643 prettyPrintAttributes(D);
646 void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
651 void DeclPrinter::VisitVarDecl(VarDecl *D) {
652 if (!Policy.SuppressSpecifiers) {
653 StorageClass SC = D->getStorageClass();
655 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
657 switch (D->getTSCSpec()) {
658 case TSCS_unspecified:
663 case TSCS__Thread_local:
664 Out << "_Thread_local ";
666 case TSCS_thread_local:
667 Out << "thread_local ";
671 if (D->isModulePrivate())
672 Out << "__module_private__ ";
675 QualType T = D->getTypeSourceInfo()
676 ? D->getTypeSourceInfo()->getType()
677 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
678 T.print(Out, Policy, D->getName());
679 Expr *Init = D->getInit();
680 if (!Policy.SuppressInitializers && Init) {
681 bool ImplicitInit = false;
682 if (CXXConstructExpr *Construct =
683 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
684 if (D->getInitStyle() == VarDecl::CallInit &&
685 !Construct->isListInitialization()) {
686 ImplicitInit = Construct->getNumArgs() == 0 ||
687 Construct->getArg(0)->isDefaultArgument();
691 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
693 else if (D->getInitStyle() == VarDecl::CInit) {
696 Init->printPretty(Out, nullptr, Policy, Indentation);
697 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
701 prettyPrintAttributes(D);
704 void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
708 void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
710 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
714 void DeclPrinter::VisitImportDecl(ImportDecl *D) {
715 Out << "@import " << D->getImportedModule()->getFullModuleName()
719 void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
720 Out << "static_assert(";
721 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
723 D->getMessage()->printPretty(Out, nullptr, Policy, Indentation);
727 //----------------------------------------------------------------------------
729 //----------------------------------------------------------------------------
730 void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
733 Out << "namespace " << *D << " {\n";
738 void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
739 Out << "using namespace ";
740 if (D->getQualifier())
741 D->getQualifier()->print(Out, Policy);
742 Out << *D->getNominatedNamespaceAsWritten();
745 void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
746 Out << "namespace " << *D << " = ";
747 if (D->getQualifier())
748 D->getQualifier()->print(Out, Policy);
749 Out << *D->getAliasedNamespace();
752 void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
753 prettyPrintAttributes(D);
756 void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
757 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
758 Out << "__module_private__ ";
759 Out << D->getKindName();
760 if (D->getIdentifier())
763 if (D->isCompleteDefinition()) {
764 // Print the base classes
765 if (D->getNumBases()) {
767 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
768 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
769 if (Base != D->bases_begin())
772 if (Base->isVirtual())
775 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
778 Out << " " << Base->getType().getAsString(Policy);
780 if (Base->isPackExpansion())
785 // Print the class definition
786 // FIXME: Doesn't print access specifiers, e.g., "public:"
793 void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
795 if (D->getLanguage() == LinkageSpecDecl::lang_c)
798 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
799 "unknown language in linkage specification");
803 Out << "extern \"" << l << "\" ";
804 if (D->hasBraces()) {
809 Visit(*D->decls_begin());
812 void DeclPrinter::PrintTemplateParameters(const TemplateParameterList *Params,
813 const TemplateArgumentList *Args) {
815 assert(!Args || Params->size() == Args->size());
819 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
823 const Decl *Param = Params->getParam(i);
824 if (const TemplateTypeParmDecl *TTP =
825 dyn_cast<TemplateTypeParmDecl>(Param)) {
827 if (TTP->wasDeclaredWithTypename())
832 if (TTP->isParameterPack())
839 Args->get(i).print(Policy, Out);
840 } else if (TTP->hasDefaultArgument()) {
842 Out << TTP->getDefaultArgument().getAsString(Policy);
844 } else if (const NonTypeTemplateParmDecl *NTTP =
845 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
846 Out << NTTP->getType().getAsString(Policy);
848 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType()))
851 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
853 Out << Name->getName();
858 Args->get(i).print(Policy, Out);
859 } else if (NTTP->hasDefaultArgument()) {
861 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
864 } else if (const TemplateTemplateParmDecl *TTPD =
865 dyn_cast<TemplateTemplateParmDecl>(Param)) {
866 VisitTemplateDecl(TTPD);
867 // FIXME: print the default argument, if present.
874 void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
875 PrintTemplateParameters(D->getTemplateParameters());
877 if (const TemplateTemplateParmDecl *TTP =
878 dyn_cast<TemplateTemplateParmDecl>(D)) {
880 if (TTP->isParameterPack())
884 Visit(D->getTemplatedDecl());
888 void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
889 if (PrintInstantiation) {
890 TemplateParameterList *Params = D->getTemplateParameters();
891 for (auto *I : D->specializations()) {
892 PrintTemplateParameters(Params, I->getTemplateSpecializationArgs());
897 return VisitRedeclarableTemplateDecl(D);
900 void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
901 if (PrintInstantiation) {
902 TemplateParameterList *Params = D->getTemplateParameters();
903 for (auto *I : D->specializations()) {
904 PrintTemplateParameters(Params, &I->getTemplateArgs());
910 return VisitRedeclarableTemplateDecl(D);
913 //----------------------------------------------------------------------------
914 // Objective-C declarations
915 //----------------------------------------------------------------------------
917 void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
918 if (OMD->isInstanceMethod())
922 if (!OMD->getReturnType().isNull())
923 Out << '(' << OMD->getASTContext()
924 .getUnqualifiedObjCPointerType(OMD->getReturnType())
925 .getAsString(Policy) << ")";
927 std::string name = OMD->getSelector().getAsString();
928 std::string::size_type pos, lastPos = 0;
929 for (const auto *PI : OMD->params()) {
930 // FIXME: selector is missing here!
931 pos = name.find_first_of(':', lastPos);
932 Out << " " << name.substr(lastPos, pos - lastPos);
933 Out << ":(" << PI->getASTContext().getUnqualifiedObjCPointerType(PI->getType()).
934 getAsString(Policy) << ')' << *PI;
938 if (OMD->param_begin() == OMD->param_end())
941 if (OMD->isVariadic())
944 if (OMD->getBody() && !Policy.TerseOutput) {
946 OMD->getBody()->printPretty(Out, nullptr, Policy);
949 else if (Policy.PolishForDeclaration)
953 void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
954 std::string I = OID->getNameAsString();
955 ObjCInterfaceDecl *SID = OID->getSuperClass();
958 Out << "@implementation " << I << " : " << *SID;
960 Out << "@implementation " << I;
962 if (OID->ivar_size() > 0) {
964 Indentation += Policy.Indentation;
965 for (const auto *I : OID->ivars()) {
966 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
967 getAsString(Policy) << ' ' << *I << ";\n";
969 Indentation -= Policy.Indentation;
972 VisitDeclContext(OID, false);
976 void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
977 std::string I = OID->getNameAsString();
978 ObjCInterfaceDecl *SID = OID->getSuperClass();
980 if (!OID->isThisDeclarationADefinition()) {
981 Out << "@class " << I << ";";
984 bool eolnOut = false;
986 Out << "@interface " << I << " : " << *SID;
988 Out << "@interface " << I;
991 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
992 if (!Protocols.empty()) {
993 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
994 E = Protocols.end(); I != E; ++I)
995 Out << (I == Protocols.begin() ? '<' : ',') << **I;
999 if (OID->ivar_size() > 0) {
1002 Indentation += Policy.Indentation;
1003 for (const auto *I : OID->ivars()) {
1004 Indent() << I->getASTContext()
1005 .getUnqualifiedObjCPointerType(I->getType())
1006 .getAsString(Policy) << ' ' << *I << ";\n";
1008 Indentation -= Policy.Indentation;
1016 VisitDeclContext(OID, false);
1020 // FIXME: implement the rest...
1023 void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
1024 if (!PID->isThisDeclarationADefinition()) {
1025 Out << "@protocol " << *PID << ";\n";
1029 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1030 if (!Protocols.empty()) {
1031 Out << "@protocol " << *PID;
1032 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1033 E = Protocols.end(); I != E; ++I)
1034 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1037 Out << "@protocol " << *PID << '\n';
1038 VisitDeclContext(PID, false);
1042 void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
1043 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
1045 VisitDeclContext(PID, false);
1047 // FIXME: implement the rest...
1050 void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
1051 Out << "@interface " << *PID->getClassInterface() << '(' << *PID << ")\n";
1052 if (PID->ivar_size() > 0) {
1054 Indentation += Policy.Indentation;
1055 for (const auto *I : PID->ivars())
1056 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
1057 getAsString(Policy) << ' ' << *I << ";\n";
1058 Indentation -= Policy.Indentation;
1062 VisitDeclContext(PID, false);
1065 // FIXME: implement the rest...
1068 void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
1069 Out << "@compatibility_alias " << *AID
1070 << ' ' << *AID->getClassInterface() << ";\n";
1073 /// PrintObjCPropertyDecl - print a property declaration.
1075 void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1076 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1077 Out << "@required\n";
1078 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1079 Out << "@optional\n";
1082 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1085 if (PDecl->getPropertyAttributes() &
1086 ObjCPropertyDecl::OBJC_PR_readonly) {
1087 Out << (first ? ' ' : ',') << "readonly";
1091 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1092 Out << (first ? ' ' : ',') << "getter = ";
1093 PDecl->getGetterName().print(Out);
1096 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1097 Out << (first ? ' ' : ',') << "setter = ";
1098 PDecl->getSetterName().print(Out);
1102 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1103 Out << (first ? ' ' : ',') << "assign";
1107 if (PDecl->getPropertyAttributes() &
1108 ObjCPropertyDecl::OBJC_PR_readwrite) {
1109 Out << (first ? ' ' : ',') << "readwrite";
1113 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1114 Out << (first ? ' ' : ',') << "retain";
1118 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1119 Out << (first ? ' ' : ',') << "strong";
1123 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1124 Out << (first ? ' ' : ',') << "copy";
1128 if (PDecl->getPropertyAttributes() &
1129 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1130 Out << (first ? ' ' : ',') << "nonatomic";
1133 if (PDecl->getPropertyAttributes() &
1134 ObjCPropertyDecl::OBJC_PR_atomic) {
1135 Out << (first ? ' ' : ',') << "atomic";
1139 (void) first; // Silence dead store warning due to idiomatic code.
1142 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(PDecl->getType()).
1143 getAsString(Policy) << ' ' << *PDecl;
1144 if (Policy.PolishForDeclaration)
1148 void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1149 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1150 Out << "@synthesize ";
1153 Out << *PID->getPropertyDecl();
1154 if (PID->getPropertyIvarDecl())
1155 Out << '=' << *PID->getPropertyIvarDecl();
1158 void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
1159 if (!D->isAccessDeclaration())
1161 if (D->hasTypename())
1163 D->getQualifier()->print(Out, Policy);
1168 DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1169 Out << "using typename ";
1170 D->getQualifier()->print(Out, Policy);
1171 Out << D->getDeclName();
1174 void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1175 if (!D->isAccessDeclaration())
1177 D->getQualifier()->print(Out, Policy);
1178 Out << D->getName();
1181 void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1185 void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1186 Out << "#pragma omp threadprivate";
1187 if (!D->varlist_empty()) {
1188 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1189 E = D->varlist_end();
1191 Out << (I == D->varlist_begin() ? '(' : ',');
1192 NamedDecl *ND = cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
1193 ND->printQualifiedName(Out);