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 = 0);
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->getResultType();
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 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 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
242 // of __builtin_va_list or __[u]int128_t. There should be some other way
244 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) {
245 if (IdentifierInfo *II = ND->getIdentifier()) {
246 if (II->isStr("__builtin_va_list") ||
247 II->isStr("__int128_t") || II->isStr("__uint128_t"))
252 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
253 // forced to merge the declarations because there's no other way to
254 // refer to the struct in question. This limited merging is safe without
255 // a bunch of other checks because it only merges declarations directly
256 // referring to the tag, not typedefs.
258 // Check whether the current declaration should be grouped with a previous
260 QualType CurDeclType = getDeclType(*D);
261 if (!Decls.empty() && !CurDeclType.isNull()) {
262 QualType BaseType = GetBaseType(CurDeclType);
263 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
264 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
270 // If we have a merged group waiting to be handled, handle it now.
272 ProcessDeclGroup(Decls);
274 // If the current declaration is an unnamed tag type, save it
275 // so we can merge it with the subsequent declaration(s) using it.
276 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
281 if (isa<AccessSpecDecl>(*D)) {
282 Indentation -= Policy.Indentation;
284 Print(D->getAccess());
286 Indentation += Policy.Indentation;
293 // FIXME: Need to be able to tell the DeclPrinter when
294 const char *Terminator = 0;
295 if (isa<OMPThreadPrivateDecl>(*D))
297 else if (isa<FunctionDecl>(*D) &&
298 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
300 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
302 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
303 isa<ObjCImplementationDecl>(*D) ||
304 isa<ObjCInterfaceDecl>(*D) ||
305 isa<ObjCProtocolDecl>(*D) ||
306 isa<ObjCCategoryImplDecl>(*D) ||
307 isa<ObjCCategoryDecl>(*D))
309 else if (isa<EnumConstantDecl>(*D)) {
310 DeclContext::decl_iterator Next = D;
323 ProcessDeclGroup(Decls);
326 Indentation -= Policy.Indentation;
329 void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
330 VisitDeclContext(D, false);
333 void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
334 if (!Policy.SuppressSpecifiers) {
337 if (D->isModulePrivate())
338 Out << "__module_private__ ";
340 D->getUnderlyingType().print(Out, Policy, D->getName());
341 prettyPrintAttributes(D);
344 void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
345 Out << "using " << *D << " = " << D->getUnderlyingType().getAsString(Policy);
348 void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
349 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
350 Out << "__module_private__ ";
353 if (D->isScopedUsingClassTag())
361 Out << " : " << D->getIntegerType().stream(Policy);
363 if (D->isCompleteDefinition()) {
368 prettyPrintAttributes(D);
371 void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
372 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
373 Out << "__module_private__ ";
374 Out << D->getKindName();
375 if (D->getIdentifier())
378 if (D->isCompleteDefinition()) {
385 void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
387 if (Expr *Init = D->getInitExpr()) {
389 Init->printPretty(Out, 0, Policy, Indentation);
393 void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
394 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
395 if (!Policy.SuppressSpecifiers) {
396 switch (D->getStorageClass()) {
398 case SC_Extern: Out << "extern "; break;
399 case SC_Static: Out << "static "; break;
400 case SC_PrivateExtern: Out << "__private_extern__ "; break;
401 case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal:
402 llvm_unreachable("invalid for functions");
405 if (D->isInlineSpecified()) Out << "inline ";
406 if (D->isVirtualAsWritten()) Out << "virtual ";
407 if (D->isModulePrivate()) Out << "__module_private__ ";
408 if (CDecl && CDecl->isExplicitSpecified())
412 PrintingPolicy SubPolicy(Policy);
413 SubPolicy.SuppressSpecifiers = false;
414 std::string Proto = D->getNameInfo().getAsString();
416 QualType Ty = D->getType();
417 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
418 Proto = '(' + Proto + ')';
419 Ty = PT->getInnerType();
422 if (isa<FunctionType>(Ty)) {
423 const FunctionType *AFT = Ty->getAs<FunctionType>();
424 const FunctionProtoType *FT = 0;
425 if (D->hasWrittenPrototype())
426 FT = dyn_cast<FunctionProtoType>(AFT);
430 llvm::raw_string_ostream POut(Proto);
431 DeclPrinter ParamPrinter(POut, SubPolicy, Indentation);
432 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
434 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
437 if (FT->isVariadic()) {
438 if (D->getNumParams()) POut << ", ";
441 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
442 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
445 Proto += D->getParamDecl(i)->getNameAsString();
454 if (FT->isVolatile())
455 Proto += " volatile";
456 if (FT->isRestrict())
457 Proto += " restrict";
460 if (FT && FT->hasDynamicExceptionSpec()) {
462 if (FT->getExceptionSpecType() == EST_MSAny)
465 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
469 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
472 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
473 Proto += " noexcept";
474 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
476 llvm::raw_string_ostream EOut(Proto);
477 FT->getNoexceptExpr()->printPretty(EOut, 0, SubPolicy,
486 bool HasInitializerList = false;
487 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
488 E = CDecl->init_end();
490 CXXCtorInitializer *BMInitializer = (*B);
491 if (BMInitializer->isInClassMemberInitializer())
494 if (!HasInitializerList) {
498 HasInitializerList = true;
502 if (BMInitializer->isAnyMemberInitializer()) {
503 FieldDecl *FD = BMInitializer->getAnyMember();
506 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
510 if (!BMInitializer->getInit()) {
513 Expr *Init = BMInitializer->getInit();
514 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
515 Init = Tmp->getSubExpr();
517 Init = Init->IgnoreParens();
519 Expr *SimpleInit = 0;
521 unsigned NumArgs = 0;
522 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
523 Args = ParenList->getExprs();
524 NumArgs = ParenList->getNumExprs();
525 } else if (CXXConstructExpr *Construct
526 = dyn_cast<CXXConstructExpr>(Init)) {
527 Args = Construct->getArgs();
528 NumArgs = Construct->getNumArgs();
533 SimpleInit->printPretty(Out, 0, Policy, Indentation);
535 for (unsigned I = 0; I != NumArgs; ++I) {
536 if (isa<CXXDefaultArgExpr>(Args[I]))
541 Args[I]->printPretty(Out, 0, Policy, Indentation);
550 if (FT && FT->hasTrailingReturn()) {
551 Out << "auto " << Proto << " -> ";
554 AFT->getResultType().print(Out, Policy, Proto);
557 Ty.print(Out, Policy, Proto);
560 prettyPrintAttributes(D);
564 else if (D->isDeletedAsWritten())
566 else if (D->isExplicitlyDefaulted())
568 else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) {
569 if (!D->hasPrototype() && D->getNumParams()) {
570 // This is a K&R function definition, so we need to print the
573 DeclPrinter ParamPrinter(Out, SubPolicy, Indentation);
574 Indentation += Policy.Indentation;
575 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
577 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
580 Indentation -= Policy.Indentation;
584 D->getBody()->printPretty(Out, 0, SubPolicy, Indentation);
589 void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
590 if (TypeSourceInfo *TSI = D->getFriendType()) {
591 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
592 for (unsigned i = 0; i < NumTPLists; ++i)
593 PrintTemplateParameters(D->getFriendTypeTemplateParameterList(i));
595 Out << " " << TSI->getType().getAsString(Policy);
597 else if (FunctionDecl *FD =
598 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
600 VisitFunctionDecl(FD);
602 else if (FunctionTemplateDecl *FTD =
603 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
605 VisitFunctionTemplateDecl(FTD);
607 else if (ClassTemplateDecl *CTD =
608 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
610 VisitRedeclarableTemplateDecl(CTD);
614 void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
615 if (!Policy.SuppressSpecifiers && D->isMutable())
617 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
618 Out << "__module_private__ ";
620 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
621 stream(Policy, D->getName());
623 if (D->isBitField()) {
625 D->getBitWidth()->printPretty(Out, 0, Policy, Indentation);
628 Expr *Init = D->getInClassInitializer();
629 if (!Policy.SuppressInitializers && Init) {
630 if (D->getInClassInitStyle() == ICIS_ListInit)
634 Init->printPretty(Out, 0, Policy, Indentation);
636 prettyPrintAttributes(D);
639 void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
644 void DeclPrinter::VisitVarDecl(VarDecl *D) {
645 if (!Policy.SuppressSpecifiers) {
646 StorageClass SC = D->getStorageClass();
648 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
650 switch (D->getTSCSpec()) {
651 case TSCS_unspecified:
656 case TSCS__Thread_local:
657 Out << "_Thread_local ";
659 case TSCS_thread_local:
660 Out << "thread_local ";
664 if (D->isModulePrivate())
665 Out << "__module_private__ ";
668 QualType T = D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
669 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D))
670 T = Parm->getOriginalType();
671 T.print(Out, Policy, D->getName());
672 Expr *Init = D->getInit();
673 if (!Policy.SuppressInitializers && Init) {
674 bool ImplicitInit = false;
675 if (CXXConstructExpr *Construct =
676 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
677 if (D->getInitStyle() == VarDecl::CallInit &&
678 !Construct->isListInitialization()) {
679 ImplicitInit = Construct->getNumArgs() == 0 ||
680 Construct->getArg(0)->isDefaultArgument();
684 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
686 else if (D->getInitStyle() == VarDecl::CInit) {
689 Init->printPretty(Out, 0, Policy, Indentation);
690 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
694 prettyPrintAttributes(D);
697 void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
701 void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
703 D->getAsmString()->printPretty(Out, 0, Policy, Indentation);
707 void DeclPrinter::VisitImportDecl(ImportDecl *D) {
708 Out << "@import " << D->getImportedModule()->getFullModuleName()
712 void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
713 Out << "static_assert(";
714 D->getAssertExpr()->printPretty(Out, 0, Policy, Indentation);
716 D->getMessage()->printPretty(Out, 0, Policy, Indentation);
720 //----------------------------------------------------------------------------
722 //----------------------------------------------------------------------------
723 void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
726 Out << "namespace " << *D << " {\n";
731 void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
732 Out << "using namespace ";
733 if (D->getQualifier())
734 D->getQualifier()->print(Out, Policy);
735 Out << *D->getNominatedNamespaceAsWritten();
738 void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
739 Out << "namespace " << *D << " = ";
740 if (D->getQualifier())
741 D->getQualifier()->print(Out, Policy);
742 Out << *D->getAliasedNamespace();
745 void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
746 prettyPrintAttributes(D);
749 void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
750 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
751 Out << "__module_private__ ";
752 Out << D->getKindName();
753 if (D->getIdentifier())
756 if (D->isCompleteDefinition()) {
757 // Print the base classes
758 if (D->getNumBases()) {
760 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
761 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
762 if (Base != D->bases_begin())
765 if (Base->isVirtual())
768 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
771 Out << " " << Base->getType().getAsString(Policy);
773 if (Base->isPackExpansion())
778 // Print the class definition
779 // FIXME: Doesn't print access specifiers, e.g., "public:"
786 void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
788 if (D->getLanguage() == LinkageSpecDecl::lang_c)
791 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
792 "unknown language in linkage specification");
796 Out << "extern \"" << l << "\" ";
797 if (D->hasBraces()) {
802 Visit(*D->decls_begin());
805 void DeclPrinter::PrintTemplateParameters(const TemplateParameterList *Params,
806 const TemplateArgumentList *Args) {
808 assert(!Args || Params->size() == Args->size());
812 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
816 const Decl *Param = Params->getParam(i);
817 if (const TemplateTypeParmDecl *TTP =
818 dyn_cast<TemplateTypeParmDecl>(Param)) {
820 if (TTP->wasDeclaredWithTypename())
825 if (TTP->isParameterPack())
832 Args->get(i).print(Policy, Out);
833 } else if (TTP->hasDefaultArgument()) {
835 Out << TTP->getDefaultArgument().getAsString(Policy);
837 } else if (const NonTypeTemplateParmDecl *NTTP =
838 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
839 Out << NTTP->getType().getAsString(Policy);
841 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType()))
844 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
846 Out << Name->getName();
851 Args->get(i).print(Policy, Out);
852 } else if (NTTP->hasDefaultArgument()) {
854 NTTP->getDefaultArgument()->printPretty(Out, 0, Policy, Indentation);
856 } else if (const TemplateTemplateParmDecl *TTPD =
857 dyn_cast<TemplateTemplateParmDecl>(Param)) {
858 VisitTemplateDecl(TTPD);
859 // FIXME: print the default argument, if present.
866 void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
867 PrintTemplateParameters(D->getTemplateParameters());
869 if (const TemplateTemplateParmDecl *TTP =
870 dyn_cast<TemplateTemplateParmDecl>(D)) {
872 if (TTP->isParameterPack())
876 Visit(D->getTemplatedDecl());
880 void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
881 if (PrintInstantiation) {
882 TemplateParameterList *Params = D->getTemplateParameters();
883 for (FunctionTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
885 PrintTemplateParameters(Params, (*I)->getTemplateSpecializationArgs());
890 return VisitRedeclarableTemplateDecl(D);
893 void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
894 if (PrintInstantiation) {
895 TemplateParameterList *Params = D->getTemplateParameters();
896 for (ClassTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
898 PrintTemplateParameters(Params, &(*I)->getTemplateArgs());
904 return VisitRedeclarableTemplateDecl(D);
907 //----------------------------------------------------------------------------
908 // Objective-C declarations
909 //----------------------------------------------------------------------------
911 void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
912 if (OMD->isInstanceMethod())
916 if (!OMD->getResultType().isNull())
917 Out << '(' << OMD->getASTContext().getUnqualifiedObjCPointerType(OMD->getResultType()).
918 getAsString(Policy) << ")";
920 std::string name = OMD->getSelector().getAsString();
921 std::string::size_type pos, lastPos = 0;
922 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
923 E = OMD->param_end(); PI != E; ++PI) {
924 // FIXME: selector is missing here!
925 pos = name.find_first_of(':', lastPos);
926 Out << " " << name.substr(lastPos, pos - lastPos);
927 Out << ":(" << (*PI)->getASTContext().getUnqualifiedObjCPointerType((*PI)->getType()).
928 getAsString(Policy) << ')' << **PI;
932 if (OMD->param_begin() == OMD->param_end())
935 if (OMD->isVariadic())
938 if (OMD->getBody() && !Policy.TerseOutput) {
940 OMD->getBody()->printPretty(Out, 0, Policy);
943 else if (Policy.PolishForDeclaration)
947 void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
948 std::string I = OID->getNameAsString();
949 ObjCInterfaceDecl *SID = OID->getSuperClass();
952 Out << "@implementation " << I << " : " << *SID;
954 Out << "@implementation " << I;
956 if (OID->ivar_size() > 0) {
958 Indentation += Policy.Indentation;
959 for (ObjCImplementationDecl::ivar_iterator I = OID->ivar_begin(),
960 E = OID->ivar_end(); I != E; ++I) {
961 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
962 getAsString(Policy) << ' ' << **I << ";\n";
964 Indentation -= Policy.Indentation;
967 VisitDeclContext(OID, false);
971 void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
972 std::string I = OID->getNameAsString();
973 ObjCInterfaceDecl *SID = OID->getSuperClass();
975 if (!OID->isThisDeclarationADefinition()) {
976 Out << "@class " << I << ";";
979 bool eolnOut = false;
981 Out << "@interface " << I << " : " << *SID;
983 Out << "@interface " << I;
986 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
987 if (!Protocols.empty()) {
988 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
989 E = Protocols.end(); I != E; ++I)
990 Out << (I == Protocols.begin() ? '<' : ',') << **I;
994 if (OID->ivar_size() > 0) {
997 Indentation += Policy.Indentation;
998 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
999 E = OID->ivar_end(); I != E; ++I) {
1000 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
1001 getAsString(Policy) << ' ' << **I << ";\n";
1003 Indentation -= Policy.Indentation;
1011 VisitDeclContext(OID, false);
1015 // FIXME: implement the rest...
1018 void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
1019 if (!PID->isThisDeclarationADefinition()) {
1020 Out << "@protocol " << *PID << ";\n";
1024 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1025 if (!Protocols.empty()) {
1026 Out << "@protocol " << *PID;
1027 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1028 E = Protocols.end(); I != E; ++I)
1029 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1032 Out << "@protocol " << *PID << '\n';
1033 VisitDeclContext(PID, false);
1037 void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
1038 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
1040 VisitDeclContext(PID, false);
1042 // FIXME: implement the rest...
1045 void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
1046 Out << "@interface " << *PID->getClassInterface() << '(' << *PID << ")\n";
1047 if (PID->ivar_size() > 0) {
1049 Indentation += Policy.Indentation;
1050 for (ObjCCategoryDecl::ivar_iterator I = PID->ivar_begin(),
1051 E = PID->ivar_end(); I != E; ++I) {
1052 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
1053 getAsString(Policy) << ' ' << **I << ";\n";
1055 Indentation -= Policy.Indentation;
1059 VisitDeclContext(PID, false);
1062 // FIXME: implement the rest...
1065 void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
1066 Out << "@compatibility_alias " << *AID
1067 << ' ' << *AID->getClassInterface() << ";\n";
1070 /// PrintObjCPropertyDecl - print a property declaration.
1072 void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1073 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1074 Out << "@required\n";
1075 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1076 Out << "@optional\n";
1079 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1082 if (PDecl->getPropertyAttributes() &
1083 ObjCPropertyDecl::OBJC_PR_readonly) {
1084 Out << (first ? ' ' : ',') << "readonly";
1088 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1089 Out << (first ? ' ' : ',') << "getter = "
1090 << PDecl->getGetterName().getAsString();
1093 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1094 Out << (first ? ' ' : ',') << "setter = "
1095 << PDecl->getSetterName().getAsString();
1099 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1100 Out << (first ? ' ' : ',') << "assign";
1104 if (PDecl->getPropertyAttributes() &
1105 ObjCPropertyDecl::OBJC_PR_readwrite) {
1106 Out << (first ? ' ' : ',') << "readwrite";
1110 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1111 Out << (first ? ' ' : ',') << "retain";
1115 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1116 Out << (first ? ' ' : ',') << "strong";
1120 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1121 Out << (first ? ' ' : ',') << "copy";
1125 if (PDecl->getPropertyAttributes() &
1126 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1127 Out << (first ? ' ' : ',') << "nonatomic";
1130 if (PDecl->getPropertyAttributes() &
1131 ObjCPropertyDecl::OBJC_PR_atomic) {
1132 Out << (first ? ' ' : ',') << "atomic";
1136 (void) first; // Silence dead store warning due to idiomatic code.
1139 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(PDecl->getType()).
1140 getAsString(Policy) << ' ' << *PDecl;
1141 if (Policy.PolishForDeclaration)
1145 void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1146 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1147 Out << "@synthesize ";
1150 Out << *PID->getPropertyDecl();
1151 if (PID->getPropertyIvarDecl())
1152 Out << '=' << *PID->getPropertyIvarDecl();
1155 void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
1157 D->getQualifier()->print(Out, Policy);
1162 DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1163 Out << "using typename ";
1164 D->getQualifier()->print(Out, Policy);
1165 Out << D->getDeclName();
1168 void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1170 D->getQualifier()->print(Out, Policy);
1171 Out << D->getName();
1174 void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1178 void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1179 Out << "#pragma omp threadprivate";
1180 if (!D->varlist_empty()) {
1181 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1182 E = D->varlist_end();
1184 Out << (I == D->varlist_begin() ? '(' : ',')
1185 << *cast<NamedDecl>((*I)->getDecl());