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<ElaboratedType>(BaseType))
264 BaseType = cast<ElaboratedType>(BaseType)->getNamedType();
265 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
266 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
272 // If we have a merged group waiting to be handled, handle it now.
274 ProcessDeclGroup(Decls);
276 // If the current declaration is an unnamed tag type, save it
277 // so we can merge it with the subsequent declaration(s) using it.
278 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
283 if (isa<AccessSpecDecl>(*D)) {
284 Indentation -= Policy.Indentation;
286 Print(D->getAccess());
288 Indentation += Policy.Indentation;
295 // FIXME: Need to be able to tell the DeclPrinter when
296 const char *Terminator = 0;
297 if (isa<OMPThreadPrivateDecl>(*D))
299 else if (isa<FunctionDecl>(*D) &&
300 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
302 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
304 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
305 isa<ObjCImplementationDecl>(*D) ||
306 isa<ObjCInterfaceDecl>(*D) ||
307 isa<ObjCProtocolDecl>(*D) ||
308 isa<ObjCCategoryImplDecl>(*D) ||
309 isa<ObjCCategoryDecl>(*D))
311 else if (isa<EnumConstantDecl>(*D)) {
312 DeclContext::decl_iterator Next = D;
325 ProcessDeclGroup(Decls);
328 Indentation -= Policy.Indentation;
331 void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
332 VisitDeclContext(D, false);
335 void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
336 if (!Policy.SuppressSpecifiers) {
339 if (D->isModulePrivate())
340 Out << "__module_private__ ";
342 D->getTypeSourceInfo()->getType().print(Out, Policy, D->getName());
343 prettyPrintAttributes(D);
346 void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
347 Out << "using " << *D;
348 prettyPrintAttributes(D);
349 Out << " = " << D->getTypeSourceInfo()->getType().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 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
399 if (!Policy.SuppressSpecifiers) {
400 switch (D->getStorageClass()) {
402 case SC_Extern: Out << "extern "; break;
403 case SC_Static: Out << "static "; break;
404 case SC_PrivateExtern: Out << "__private_extern__ "; break;
405 case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal:
406 llvm_unreachable("invalid for functions");
409 if (D->isInlineSpecified()) Out << "inline ";
410 if (D->isVirtualAsWritten()) Out << "virtual ";
411 if (D->isModulePrivate()) Out << "__module_private__ ";
412 if (CDecl && CDecl->isExplicitSpecified())
416 PrintingPolicy SubPolicy(Policy);
417 SubPolicy.SuppressSpecifiers = false;
418 std::string Proto = D->getNameInfo().getAsString();
420 QualType Ty = D->getType();
421 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
422 Proto = '(' + Proto + ')';
423 Ty = PT->getInnerType();
426 if (isa<FunctionType>(Ty)) {
427 const FunctionType *AFT = Ty->getAs<FunctionType>();
428 const FunctionProtoType *FT = 0;
429 if (D->hasWrittenPrototype())
430 FT = dyn_cast<FunctionProtoType>(AFT);
434 llvm::raw_string_ostream POut(Proto);
435 DeclPrinter ParamPrinter(POut, SubPolicy, Indentation);
436 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
438 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
441 if (FT->isVariadic()) {
442 if (D->getNumParams()) POut << ", ";
445 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
446 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
449 Proto += D->getParamDecl(i)->getNameAsString();
458 if (FT->isVolatile())
459 Proto += " volatile";
460 if (FT->isRestrict())
461 Proto += " restrict";
464 if (FT && FT->hasDynamicExceptionSpec()) {
466 if (FT->getExceptionSpecType() == EST_MSAny)
469 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
473 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
476 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
477 Proto += " noexcept";
478 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
480 llvm::raw_string_ostream EOut(Proto);
481 FT->getNoexceptExpr()->printPretty(EOut, 0, SubPolicy,
490 bool HasInitializerList = false;
491 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
492 E = CDecl->init_end();
494 CXXCtorInitializer *BMInitializer = (*B);
495 if (BMInitializer->isInClassMemberInitializer())
498 if (!HasInitializerList) {
502 HasInitializerList = true;
506 if (BMInitializer->isAnyMemberInitializer()) {
507 FieldDecl *FD = BMInitializer->getAnyMember();
510 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
514 if (!BMInitializer->getInit()) {
517 Expr *Init = BMInitializer->getInit();
518 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
519 Init = Tmp->getSubExpr();
521 Init = Init->IgnoreParens();
523 Expr *SimpleInit = 0;
525 unsigned NumArgs = 0;
526 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
527 Args = ParenList->getExprs();
528 NumArgs = ParenList->getNumExprs();
529 } else if (CXXConstructExpr *Construct
530 = dyn_cast<CXXConstructExpr>(Init)) {
531 Args = Construct->getArgs();
532 NumArgs = Construct->getNumArgs();
537 SimpleInit->printPretty(Out, 0, Policy, Indentation);
539 for (unsigned I = 0; I != NumArgs; ++I) {
540 if (isa<CXXDefaultArgExpr>(Args[I]))
545 Args[I]->printPretty(Out, 0, Policy, Indentation);
554 if (FT && FT->hasTrailingReturn()) {
555 Out << "auto " << Proto << " -> ";
558 AFT->getResultType().print(Out, Policy, Proto);
561 Ty.print(Out, Policy, Proto);
564 prettyPrintAttributes(D);
568 else if (D->isDeletedAsWritten())
570 else if (D->isExplicitlyDefaulted())
572 else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) {
573 if (!D->hasPrototype() && D->getNumParams()) {
574 // This is a K&R function definition, so we need to print the
577 DeclPrinter ParamPrinter(Out, SubPolicy, Indentation);
578 Indentation += Policy.Indentation;
579 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
581 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
584 Indentation -= Policy.Indentation;
588 D->getBody()->printPretty(Out, 0, SubPolicy, Indentation);
593 void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
594 if (TypeSourceInfo *TSI = D->getFriendType()) {
595 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
596 for (unsigned i = 0; i < NumTPLists; ++i)
597 PrintTemplateParameters(D->getFriendTypeTemplateParameterList(i));
599 Out << " " << TSI->getType().getAsString(Policy);
601 else if (FunctionDecl *FD =
602 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
604 VisitFunctionDecl(FD);
606 else if (FunctionTemplateDecl *FTD =
607 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
609 VisitFunctionTemplateDecl(FTD);
611 else if (ClassTemplateDecl *CTD =
612 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
614 VisitRedeclarableTemplateDecl(CTD);
618 void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
619 if (!Policy.SuppressSpecifiers && D->isMutable())
621 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
622 Out << "__module_private__ ";
624 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
625 stream(Policy, D->getName());
627 if (D->isBitField()) {
629 D->getBitWidth()->printPretty(Out, 0, Policy, Indentation);
632 Expr *Init = D->getInClassInitializer();
633 if (!Policy.SuppressInitializers && Init) {
634 if (D->getInClassInitStyle() == ICIS_ListInit)
638 Init->printPretty(Out, 0, Policy, Indentation);
640 prettyPrintAttributes(D);
643 void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
648 void DeclPrinter::VisitVarDecl(VarDecl *D) {
649 if (!Policy.SuppressSpecifiers) {
650 StorageClass SC = D->getStorageClass();
652 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
654 switch (D->getTSCSpec()) {
655 case TSCS_unspecified:
660 case TSCS__Thread_local:
661 Out << "_Thread_local ";
663 case TSCS_thread_local:
664 Out << "thread_local ";
668 if (D->isModulePrivate())
669 Out << "__module_private__ ";
672 QualType T = D->getTypeSourceInfo()
673 ? D->getTypeSourceInfo()->getType()
674 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
675 T.print(Out, Policy, D->getName());
676 Expr *Init = D->getInit();
677 if (!Policy.SuppressInitializers && Init) {
678 bool ImplicitInit = false;
679 if (CXXConstructExpr *Construct =
680 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
681 if (D->getInitStyle() == VarDecl::CallInit &&
682 !Construct->isListInitialization()) {
683 ImplicitInit = Construct->getNumArgs() == 0 ||
684 Construct->getArg(0)->isDefaultArgument();
688 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
690 else if (D->getInitStyle() == VarDecl::CInit) {
693 Init->printPretty(Out, 0, Policy, Indentation);
694 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
698 prettyPrintAttributes(D);
701 void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
705 void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
707 D->getAsmString()->printPretty(Out, 0, Policy, Indentation);
711 void DeclPrinter::VisitImportDecl(ImportDecl *D) {
712 Out << "@import " << D->getImportedModule()->getFullModuleName()
716 void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
717 Out << "static_assert(";
718 D->getAssertExpr()->printPretty(Out, 0, Policy, Indentation);
720 D->getMessage()->printPretty(Out, 0, Policy, Indentation);
724 //----------------------------------------------------------------------------
726 //----------------------------------------------------------------------------
727 void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
730 Out << "namespace " << *D << " {\n";
735 void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
736 Out << "using namespace ";
737 if (D->getQualifier())
738 D->getQualifier()->print(Out, Policy);
739 Out << *D->getNominatedNamespaceAsWritten();
742 void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
743 Out << "namespace " << *D << " = ";
744 if (D->getQualifier())
745 D->getQualifier()->print(Out, Policy);
746 Out << *D->getAliasedNamespace();
749 void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
750 prettyPrintAttributes(D);
753 void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
754 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
755 Out << "__module_private__ ";
756 Out << D->getKindName();
757 if (D->getIdentifier())
760 if (D->isCompleteDefinition()) {
761 // Print the base classes
762 if (D->getNumBases()) {
764 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
765 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
766 if (Base != D->bases_begin())
769 if (Base->isVirtual())
772 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
775 Out << " " << Base->getType().getAsString(Policy);
777 if (Base->isPackExpansion())
782 // Print the class definition
783 // FIXME: Doesn't print access specifiers, e.g., "public:"
790 void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
792 if (D->getLanguage() == LinkageSpecDecl::lang_c)
795 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
796 "unknown language in linkage specification");
800 Out << "extern \"" << l << "\" ";
801 if (D->hasBraces()) {
806 Visit(*D->decls_begin());
809 void DeclPrinter::PrintTemplateParameters(const TemplateParameterList *Params,
810 const TemplateArgumentList *Args) {
812 assert(!Args || Params->size() == Args->size());
816 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
820 const Decl *Param = Params->getParam(i);
821 if (const TemplateTypeParmDecl *TTP =
822 dyn_cast<TemplateTypeParmDecl>(Param)) {
824 if (TTP->wasDeclaredWithTypename())
829 if (TTP->isParameterPack())
836 Args->get(i).print(Policy, Out);
837 } else if (TTP->hasDefaultArgument()) {
839 Out << TTP->getDefaultArgument().getAsString(Policy);
841 } else if (const NonTypeTemplateParmDecl *NTTP =
842 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
843 Out << NTTP->getType().getAsString(Policy);
845 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType()))
848 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
850 Out << Name->getName();
855 Args->get(i).print(Policy, Out);
856 } else if (NTTP->hasDefaultArgument()) {
858 NTTP->getDefaultArgument()->printPretty(Out, 0, Policy, Indentation);
860 } else if (const TemplateTemplateParmDecl *TTPD =
861 dyn_cast<TemplateTemplateParmDecl>(Param)) {
862 VisitTemplateDecl(TTPD);
863 // FIXME: print the default argument, if present.
870 void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
871 PrintTemplateParameters(D->getTemplateParameters());
873 if (const TemplateTemplateParmDecl *TTP =
874 dyn_cast<TemplateTemplateParmDecl>(D)) {
876 if (TTP->isParameterPack())
880 Visit(D->getTemplatedDecl());
884 void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
885 if (PrintInstantiation) {
886 TemplateParameterList *Params = D->getTemplateParameters();
887 for (FunctionTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
889 PrintTemplateParameters(Params, (*I)->getTemplateSpecializationArgs());
894 return VisitRedeclarableTemplateDecl(D);
897 void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
898 if (PrintInstantiation) {
899 TemplateParameterList *Params = D->getTemplateParameters();
900 for (ClassTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
902 PrintTemplateParameters(Params, &(*I)->getTemplateArgs());
908 return VisitRedeclarableTemplateDecl(D);
911 //----------------------------------------------------------------------------
912 // Objective-C declarations
913 //----------------------------------------------------------------------------
915 void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
916 if (OMD->isInstanceMethod())
920 if (!OMD->getResultType().isNull())
921 Out << '(' << OMD->getASTContext().getUnqualifiedObjCPointerType(OMD->getResultType()).
922 getAsString(Policy) << ")";
924 std::string name = OMD->getSelector().getAsString();
925 std::string::size_type pos, lastPos = 0;
926 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
927 E = OMD->param_end(); PI != E; ++PI) {
928 // FIXME: selector is missing here!
929 pos = name.find_first_of(':', lastPos);
930 Out << " " << name.substr(lastPos, pos - lastPos);
931 Out << ":(" << (*PI)->getASTContext().getUnqualifiedObjCPointerType((*PI)->getType()).
932 getAsString(Policy) << ')' << **PI;
936 if (OMD->param_begin() == OMD->param_end())
939 if (OMD->isVariadic())
942 if (OMD->getBody() && !Policy.TerseOutput) {
944 OMD->getBody()->printPretty(Out, 0, Policy);
947 else if (Policy.PolishForDeclaration)
951 void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
952 std::string I = OID->getNameAsString();
953 ObjCInterfaceDecl *SID = OID->getSuperClass();
956 Out << "@implementation " << I << " : " << *SID;
958 Out << "@implementation " << I;
960 if (OID->ivar_size() > 0) {
962 Indentation += Policy.Indentation;
963 for (ObjCImplementationDecl::ivar_iterator I = OID->ivar_begin(),
964 E = OID->ivar_end(); I != E; ++I) {
965 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
966 getAsString(Policy) << ' ' << **I << ";\n";
968 Indentation -= Policy.Indentation;
971 VisitDeclContext(OID, false);
975 void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
976 std::string I = OID->getNameAsString();
977 ObjCInterfaceDecl *SID = OID->getSuperClass();
979 if (!OID->isThisDeclarationADefinition()) {
980 Out << "@class " << I << ";";
983 bool eolnOut = false;
985 Out << "@interface " << I << " : " << *SID;
987 Out << "@interface " << I;
990 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
991 if (!Protocols.empty()) {
992 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
993 E = Protocols.end(); I != E; ++I)
994 Out << (I == Protocols.begin() ? '<' : ',') << **I;
998 if (OID->ivar_size() > 0) {
1001 Indentation += Policy.Indentation;
1002 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
1003 E = OID->ivar_end(); I != E; ++I) {
1004 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
1005 getAsString(Policy) << ' ' << **I << ";\n";
1007 Indentation -= Policy.Indentation;
1015 VisitDeclContext(OID, false);
1019 // FIXME: implement the rest...
1022 void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
1023 if (!PID->isThisDeclarationADefinition()) {
1024 Out << "@protocol " << *PID << ";\n";
1028 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1029 if (!Protocols.empty()) {
1030 Out << "@protocol " << *PID;
1031 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1032 E = Protocols.end(); I != E; ++I)
1033 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1036 Out << "@protocol " << *PID << '\n';
1037 VisitDeclContext(PID, false);
1041 void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
1042 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
1044 VisitDeclContext(PID, false);
1046 // FIXME: implement the rest...
1049 void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
1050 Out << "@interface " << *PID->getClassInterface() << '(' << *PID << ")\n";
1051 if (PID->ivar_size() > 0) {
1053 Indentation += Policy.Indentation;
1054 for (ObjCCategoryDecl::ivar_iterator I = PID->ivar_begin(),
1055 E = PID->ivar_end(); I != E; ++I) {
1056 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
1057 getAsString(Policy) << ' ' << **I << ";\n";
1059 Indentation -= Policy.Indentation;
1063 VisitDeclContext(PID, false);
1066 // FIXME: implement the rest...
1069 void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
1070 Out << "@compatibility_alias " << *AID
1071 << ' ' << *AID->getClassInterface() << ";\n";
1074 /// PrintObjCPropertyDecl - print a property declaration.
1076 void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1077 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1078 Out << "@required\n";
1079 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1080 Out << "@optional\n";
1083 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1086 if (PDecl->getPropertyAttributes() &
1087 ObjCPropertyDecl::OBJC_PR_readonly) {
1088 Out << (first ? ' ' : ',') << "readonly";
1092 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1093 Out << (first ? ' ' : ',') << "getter = "
1094 << PDecl->getGetterName().getAsString();
1097 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1098 Out << (first ? ' ' : ',') << "setter = "
1099 << PDecl->getSetterName().getAsString();
1103 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1104 Out << (first ? ' ' : ',') << "assign";
1108 if (PDecl->getPropertyAttributes() &
1109 ObjCPropertyDecl::OBJC_PR_readwrite) {
1110 Out << (first ? ' ' : ',') << "readwrite";
1114 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1115 Out << (first ? ' ' : ',') << "retain";
1119 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1120 Out << (first ? ' ' : ',') << "strong";
1124 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1125 Out << (first ? ' ' : ',') << "copy";
1129 if (PDecl->getPropertyAttributes() &
1130 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1131 Out << (first ? ' ' : ',') << "nonatomic";
1134 if (PDecl->getPropertyAttributes() &
1135 ObjCPropertyDecl::OBJC_PR_atomic) {
1136 Out << (first ? ' ' : ',') << "atomic";
1140 (void) first; // Silence dead store warning due to idiomatic code.
1143 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(PDecl->getType()).
1144 getAsString(Policy) << ' ' << *PDecl;
1145 if (Policy.PolishForDeclaration)
1149 void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1150 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1151 Out << "@synthesize ";
1154 Out << *PID->getPropertyDecl();
1155 if (PID->getPropertyIvarDecl())
1156 Out << '=' << *PID->getPropertyIvarDecl();
1159 void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
1160 if (!D->isAccessDeclaration())
1162 if (D->hasTypename())
1164 D->getQualifier()->print(Out, Policy);
1169 DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1170 Out << "using typename ";
1171 D->getQualifier()->print(Out, Policy);
1172 Out << D->getDeclName();
1175 void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1176 if (!D->isAccessDeclaration())
1178 D->getQualifier()->print(Out, Policy);
1179 Out << D->getName();
1182 void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1186 void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1187 Out << "#pragma omp threadprivate";
1188 if (!D->varlist_empty()) {
1189 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1190 E = D->varlist_end();
1192 Out << (I == D->varlist_begin() ? '(' : ',');
1193 NamedDecl *ND = cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
1194 ND->printQualifiedName(Out);