1 //===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Decl::print method, which pretty prints the
11 // AST back out to C/Objective-C/C++/Objective-C++ code.
12 //
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;
26 
27 namespace {
28   class DeclPrinter : public DeclVisitor<DeclPrinter> {
29     raw_ostream &Out;
30     PrintingPolicy Policy;
31     unsigned Indentation;
32     bool PrintInstantiation;
33 
34     raw_ostream& Indent() { return Indent(Indentation); }
35     raw_ostream& Indent(unsigned Indentation);
36     void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls);
37 
38     void Print(AccessSpecifier AS);
39 
40     /// Print an Objective-C method type in parentheses.
41     ///
42     /// \param Quals The Objective-C declaration qualifiers.
43     /// \param T The type to print.
44     void PrintObjCMethodType(ASTContext &Ctx, Decl::ObjCDeclQualifier Quals,
45                              QualType T);
46 
47     void PrintObjCTypeParams(ObjCTypeParamList *Params);
48 
49   public:
50     DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy,
51                 unsigned Indentation = 0, bool PrintInstantiation = false)
52       : Out(Out), Policy(Policy), Indentation(Indentation),
53         PrintInstantiation(PrintInstantiation) { }
54 
55     void VisitDeclContext(DeclContext *DC, bool Indent = true);
56 
57     void VisitTranslationUnitDecl(TranslationUnitDecl *D);
58     void VisitTypedefDecl(TypedefDecl *D);
59     void VisitTypeAliasDecl(TypeAliasDecl *D);
60     void VisitEnumDecl(EnumDecl *D);
61     void VisitRecordDecl(RecordDecl *D);
62     void VisitEnumConstantDecl(EnumConstantDecl *D);
63     void VisitEmptyDecl(EmptyDecl *D);
64     void VisitFunctionDecl(FunctionDecl *D);
65     void VisitFriendDecl(FriendDecl *D);
66     void VisitFieldDecl(FieldDecl *D);
67     void VisitVarDecl(VarDecl *D);
68     void VisitLabelDecl(LabelDecl *D);
69     void VisitParmVarDecl(ParmVarDecl *D);
70     void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
71     void VisitImportDecl(ImportDecl *D);
72     void VisitStaticAssertDecl(StaticAssertDecl *D);
73     void VisitNamespaceDecl(NamespaceDecl *D);
74     void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
75     void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
76     void VisitCXXRecordDecl(CXXRecordDecl *D);
77     void VisitLinkageSpecDecl(LinkageSpecDecl *D);
78     void VisitTemplateDecl(const TemplateDecl *D);
79     void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
80     void VisitClassTemplateDecl(ClassTemplateDecl *D);
81     void VisitObjCMethodDecl(ObjCMethodDecl *D);
82     void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
83     void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
84     void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
85     void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
86     void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
87     void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
88     void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
89     void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
90     void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
91     void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
92     void VisitUsingDecl(UsingDecl *D);
93     void VisitUsingShadowDecl(UsingShadowDecl *D);
94     void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
95     void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
96     void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
97 
98     void PrintTemplateParameters(const TemplateParameterList *Params,
99                                  const TemplateArgumentList *Args = nullptr);
100     void prettyPrintAttributes(Decl *D);
101     void prettyPrintPragmas(Decl *D);
102     void printDeclType(QualType T, StringRef DeclName, bool Pack = false);
103   };
104 }
105 
106 void Decl::print(raw_ostream &Out, unsigned Indentation,
107                  bool PrintInstantiation) const {
108   print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
109 }
110 
111 void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
112                  unsigned Indentation, bool PrintInstantiation) const {
113   DeclPrinter Printer(Out, Policy, Indentation, PrintInstantiation);
114   Printer.Visit(const_cast<Decl*>(this));
115 }
116 
117 static QualType GetBaseType(QualType T) {
118   // FIXME: This should be on the Type class!
119   QualType BaseType = T;
120   while (!BaseType->isSpecifierType()) {
121     if (isa<TypedefType>(BaseType))
122       break;
123     else if (const PointerType* PTy = BaseType->getAs<PointerType>())
124       BaseType = PTy->getPointeeType();
125     else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
126       BaseType = BPy->getPointeeType();
127     else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
128       BaseType = ATy->getElementType();
129     else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
130       BaseType = FTy->getReturnType();
131     else if (const VectorType *VTy = BaseType->getAs<VectorType>())
132       BaseType = VTy->getElementType();
133     else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
134       BaseType = RTy->getPointeeType();
135     else if (const AutoType *ATy = BaseType->getAs<AutoType>())
136       BaseType = ATy->getDeducedType();
137     else
138       llvm_unreachable("Unknown declarator!");
139   }
140   return BaseType;
141 }
142 
143 static QualType getDeclType(Decl* D) {
144   if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
145     return TDD->getUnderlyingType();
146   if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
147     return VD->getType();
148   return QualType();
149 }
150 
151 void Decl::printGroup(Decl** Begin, unsigned NumDecls,
152                       raw_ostream &Out, const PrintingPolicy &Policy,
153                       unsigned Indentation) {
154   if (NumDecls == 1) {
155     (*Begin)->print(Out, Policy, Indentation);
156     return;
157   }
158 
159   Decl** End = Begin + NumDecls;
160   TagDecl* TD = dyn_cast<TagDecl>(*Begin);
161   if (TD)
162     ++Begin;
163 
164   PrintingPolicy SubPolicy(Policy);
165 
166   bool isFirst = true;
167   for ( ; Begin != End; ++Begin) {
168     if (isFirst) {
169       if(TD)
170         SubPolicy.IncludeTagDefinition = true;
171       SubPolicy.SuppressSpecifiers = false;
172       isFirst = false;
173     } else {
174       if (!isFirst) Out << ", ";
175       SubPolicy.IncludeTagDefinition = false;
176       SubPolicy.SuppressSpecifiers = true;
177     }
178 
179     (*Begin)->print(Out, SubPolicy, Indentation);
180   }
181 }
182 
183 LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const {
184   // Get the translation unit
185   const DeclContext *DC = this;
186   while (!DC->isTranslationUnit())
187     DC = DC->getParent();
188 
189   ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
190   DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), 0);
191   Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
192 }
193 
194 raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
195   for (unsigned i = 0; i != Indentation; ++i)
196     Out << "  ";
197   return Out;
198 }
199 
200 void DeclPrinter::prettyPrintAttributes(Decl *D) {
201   if (Policy.PolishForDeclaration)
202     return;
203 
204   if (D->hasAttrs()) {
205     AttrVec &Attrs = D->getAttrs();
206     for (auto *A : Attrs) {
207       switch (A->getKind()) {
208 #define ATTR(X)
209 #define PRAGMA_SPELLING_ATTR(X) case attr::X:
210 #include "clang/Basic/AttrList.inc"
211         break;
212       default:
213         A->printPretty(Out, Policy);
214         break;
215       }
216     }
217   }
218 }
219 
220 void DeclPrinter::prettyPrintPragmas(Decl *D) {
221   if (Policy.PolishForDeclaration)
222     return;
223 
224   if (D->hasAttrs()) {
225     AttrVec &Attrs = D->getAttrs();
226     for (auto *A : Attrs) {
227       switch (A->getKind()) {
228 #define ATTR(X)
229 #define PRAGMA_SPELLING_ATTR(X) case attr::X:
230 #include "clang/Basic/AttrList.inc"
231         A->printPretty(Out, Policy);
232         Indent();
233         break;
234       default:
235         break;
236       }
237     }
238   }
239 }
240 
241 void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) {
242   // Normally, a PackExpansionType is written as T[3]... (for instance, as a
243   // template argument), but if it is the type of a declaration, the ellipsis
244   // is placed before the name being declared.
245   if (auto *PET = T->getAs<PackExpansionType>()) {
246     Pack = true;
247     T = PET->getPattern();
248   }
249   T.print(Out, Policy, (Pack ? "..." : "") + DeclName, Indentation);
250 }
251 
252 void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
253   this->Indent();
254   Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
255   Out << ";\n";
256   Decls.clear();
257 
258 }
259 
260 void DeclPrinter::Print(AccessSpecifier AS) {
261   switch(AS) {
262   case AS_none:      llvm_unreachable("No access specifier!");
263   case AS_public:    Out << "public"; break;
264   case AS_protected: Out << "protected"; break;
265   case AS_private:   Out << "private"; break;
266   }
267 }
268 
269 //----------------------------------------------------------------------------
270 // Common C declarations
271 //----------------------------------------------------------------------------
272 
273 void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
274   if (Policy.TerseOutput)
275     return;
276 
277   if (Indent)
278     Indentation += Policy.Indentation;
279 
280   SmallVector<Decl*, 2> Decls;
281   for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
282        D != DEnd; ++D) {
283 
284     // Don't print ObjCIvarDecls, as they are printed when visiting the
285     // containing ObjCInterfaceDecl.
286     if (isa<ObjCIvarDecl>(*D))
287       continue;
288 
289     // Skip over implicit declarations in pretty-printing mode.
290     if (D->isImplicit())
291       continue;
292 
293     // The next bits of code handles stuff like "struct {int x;} a,b"; we're
294     // forced to merge the declarations because there's no other way to
295     // refer to the struct in question.  This limited merging is safe without
296     // a bunch of other checks because it only merges declarations directly
297     // referring to the tag, not typedefs.
298     //
299     // Check whether the current declaration should be grouped with a previous
300     // unnamed struct.
301     QualType CurDeclType = getDeclType(*D);
302     if (!Decls.empty() && !CurDeclType.isNull()) {
303       QualType BaseType = GetBaseType(CurDeclType);
304       if (!BaseType.isNull() && isa<ElaboratedType>(BaseType))
305         BaseType = cast<ElaboratedType>(BaseType)->getNamedType();
306       if (!BaseType.isNull() && isa<TagType>(BaseType) &&
307           cast<TagType>(BaseType)->getDecl() == Decls[0]) {
308         Decls.push_back(*D);
309         continue;
310       }
311     }
312 
313     // If we have a merged group waiting to be handled, handle it now.
314     if (!Decls.empty())
315       ProcessDeclGroup(Decls);
316 
317     // If the current declaration is an unnamed tag type, save it
318     // so we can merge it with the subsequent declaration(s) using it.
319     if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
320       Decls.push_back(*D);
321       continue;
322     }
323 
324     if (isa<AccessSpecDecl>(*D)) {
325       Indentation -= Policy.Indentation;
326       this->Indent();
327       Print(D->getAccess());
328       Out << ":\n";
329       Indentation += Policy.Indentation;
330       continue;
331     }
332 
333     this->Indent();
334     Visit(*D);
335 
336     // FIXME: Need to be able to tell the DeclPrinter when
337     const char *Terminator = nullptr;
338     if (isa<OMPThreadPrivateDecl>(*D) || isa<OMPDeclareReductionDecl>(*D))
339       Terminator = nullptr;
340     else if (isa<FunctionDecl>(*D) && cast<FunctionDecl>(*D)->hasBody())
341       Terminator = nullptr;
342     else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->hasBody())
343       Terminator = nullptr;
344     else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
345              isa<ObjCImplementationDecl>(*D) ||
346              isa<ObjCInterfaceDecl>(*D) ||
347              isa<ObjCProtocolDecl>(*D) ||
348              isa<ObjCCategoryImplDecl>(*D) ||
349              isa<ObjCCategoryDecl>(*D))
350       Terminator = nullptr;
351     else if (isa<EnumConstantDecl>(*D)) {
352       DeclContext::decl_iterator Next = D;
353       ++Next;
354       if (Next != DEnd)
355         Terminator = ",";
356     } else
357       Terminator = ";";
358 
359     if (Terminator)
360       Out << Terminator;
361     Out << "\n";
362 
363     // Declare target attribute is special one, natural spelling for the pragma
364     // assumes "ending" construct so print it here.
365     if (D->hasAttr<OMPDeclareTargetDeclAttr>())
366       Out << "#pragma omp end declare target\n";
367   }
368 
369   if (!Decls.empty())
370     ProcessDeclGroup(Decls);
371 
372   if (Indent)
373     Indentation -= Policy.Indentation;
374 }
375 
376 void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
377   VisitDeclContext(D, false);
378 }
379 
380 void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
381   if (!Policy.SuppressSpecifiers) {
382     Out << "typedef ";
383 
384     if (D->isModulePrivate())
385       Out << "__module_private__ ";
386   }
387   QualType Ty = D->getTypeSourceInfo()->getType();
388   Ty.print(Out, Policy, D->getName(), Indentation);
389   prettyPrintAttributes(D);
390 }
391 
392 void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
393   Out << "using " << *D;
394   prettyPrintAttributes(D);
395   Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
396 }
397 
398 void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
399   if (!Policy.SuppressSpecifiers && D->isModulePrivate())
400     Out << "__module_private__ ";
401   Out << "enum ";
402   if (D->isScoped()) {
403     if (D->isScopedUsingClassTag())
404       Out << "class ";
405     else
406       Out << "struct ";
407   }
408   Out << *D;
409 
410   if (D->isFixed())
411     Out << " : " << D->getIntegerType().stream(Policy);
412 
413   if (D->isCompleteDefinition()) {
414     Out << " {\n";
415     VisitDeclContext(D);
416     Indent() << "}";
417   }
418   prettyPrintAttributes(D);
419 }
420 
421 void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
422   if (!Policy.SuppressSpecifiers && D->isModulePrivate())
423     Out << "__module_private__ ";
424   Out << D->getKindName();
425 
426   prettyPrintAttributes(D);
427 
428   if (D->getIdentifier())
429     Out << ' ' << *D;
430 
431   if (D->isCompleteDefinition()) {
432     Out << " {\n";
433     VisitDeclContext(D);
434     Indent() << "}";
435   }
436 }
437 
438 void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
439   Out << *D;
440   if (Expr *Init = D->getInitExpr()) {
441     Out << " = ";
442     Init->printPretty(Out, nullptr, Policy, Indentation);
443   }
444 }
445 
446 void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
447   if (!D->getDescribedFunctionTemplate() &&
448       !D->isFunctionTemplateSpecialization())
449     prettyPrintPragmas(D);
450 
451   CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
452   CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
453   if (!Policy.SuppressSpecifiers) {
454     switch (D->getStorageClass()) {
455     case SC_None: break;
456     case SC_Extern: Out << "extern "; break;
457     case SC_Static: Out << "static "; break;
458     case SC_PrivateExtern: Out << "__private_extern__ "; break;
459     case SC_Auto: case SC_Register:
460       llvm_unreachable("invalid for functions");
461     }
462 
463     if (D->isInlineSpecified())  Out << "inline ";
464     if (D->isVirtualAsWritten()) Out << "virtual ";
465     if (D->isModulePrivate())    Out << "__module_private__ ";
466     if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr ";
467     if ((CDecl && CDecl->isExplicitSpecified()) ||
468         (ConversionDecl && ConversionDecl->isExplicit()))
469       Out << "explicit ";
470   }
471 
472   PrintingPolicy SubPolicy(Policy);
473   SubPolicy.SuppressSpecifiers = false;
474   std::string Proto = D->getNameInfo().getAsString();
475 
476   QualType Ty = D->getType();
477   while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
478     Proto = '(' + Proto + ')';
479     Ty = PT->getInnerType();
480   }
481 
482   if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
483     const FunctionProtoType *FT = nullptr;
484     if (D->hasWrittenPrototype())
485       FT = dyn_cast<FunctionProtoType>(AFT);
486 
487     Proto += "(";
488     if (FT) {
489       llvm::raw_string_ostream POut(Proto);
490       DeclPrinter ParamPrinter(POut, SubPolicy, Indentation);
491       for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
492         if (i) POut << ", ";
493         ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
494       }
495 
496       if (FT->isVariadic()) {
497         if (D->getNumParams()) POut << ", ";
498         POut << "...";
499       }
500     } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
501       for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
502         if (i)
503           Proto += ", ";
504         Proto += D->getParamDecl(i)->getNameAsString();
505       }
506     }
507 
508     Proto += ")";
509 
510     if (FT) {
511       if (FT->isConst())
512         Proto += " const";
513       if (FT->isVolatile())
514         Proto += " volatile";
515       if (FT->isRestrict())
516         Proto += " restrict";
517 
518       switch (FT->getRefQualifier()) {
519       case RQ_None:
520         break;
521       case RQ_LValue:
522         Proto += " &";
523         break;
524       case RQ_RValue:
525         Proto += " &&";
526         break;
527       }
528     }
529 
530     if (FT && FT->hasDynamicExceptionSpec()) {
531       Proto += " throw(";
532       if (FT->getExceptionSpecType() == EST_MSAny)
533         Proto += "...";
534       else
535         for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
536           if (I)
537             Proto += ", ";
538 
539           Proto += FT->getExceptionType(I).getAsString(SubPolicy);
540         }
541       Proto += ")";
542     } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
543       Proto += " noexcept";
544       if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
545         Proto += "(";
546         llvm::raw_string_ostream EOut(Proto);
547         FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
548                                            Indentation);
549         EOut.flush();
550         Proto += EOut.str();
551         Proto += ")";
552       }
553     }
554 
555     if (CDecl) {
556       bool HasInitializerList = false;
557       for (const auto *BMInitializer : CDecl->inits()) {
558         if (BMInitializer->isInClassMemberInitializer())
559           continue;
560 
561         if (!HasInitializerList) {
562           Proto += " : ";
563           Out << Proto;
564           Proto.clear();
565           HasInitializerList = true;
566         } else
567           Out << ", ";
568 
569         if (BMInitializer->isAnyMemberInitializer()) {
570           FieldDecl *FD = BMInitializer->getAnyMember();
571           Out << *FD;
572         } else {
573           Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
574         }
575 
576         Out << "(";
577         if (!BMInitializer->getInit()) {
578           // Nothing to print
579         } else {
580           Expr *Init = BMInitializer->getInit();
581           if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
582             Init = Tmp->getSubExpr();
583 
584           Init = Init->IgnoreParens();
585 
586           Expr *SimpleInit = nullptr;
587           Expr **Args = nullptr;
588           unsigned NumArgs = 0;
589           if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
590             Args = ParenList->getExprs();
591             NumArgs = ParenList->getNumExprs();
592           } else if (CXXConstructExpr *Construct
593                                         = dyn_cast<CXXConstructExpr>(Init)) {
594             Args = Construct->getArgs();
595             NumArgs = Construct->getNumArgs();
596           } else
597             SimpleInit = Init;
598 
599           if (SimpleInit)
600             SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
601           else {
602             for (unsigned I = 0; I != NumArgs; ++I) {
603               assert(Args[I] != nullptr && "Expected non-null Expr");
604               if (isa<CXXDefaultArgExpr>(Args[I]))
605                 break;
606 
607               if (I)
608                 Out << ", ";
609               Args[I]->printPretty(Out, nullptr, Policy, Indentation);
610             }
611           }
612         }
613         Out << ")";
614         if (BMInitializer->isPackExpansion())
615           Out << "...";
616       }
617     } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
618       if (FT && FT->hasTrailingReturn()) {
619         Out << "auto " << Proto << " -> ";
620         Proto.clear();
621       }
622       AFT->getReturnType().print(Out, Policy, Proto);
623       Proto.clear();
624     }
625     Out << Proto;
626   } else {
627     Ty.print(Out, Policy, Proto);
628   }
629 
630   prettyPrintAttributes(D);
631 
632   if (D->isPure())
633     Out << " = 0";
634   else if (D->isDeletedAsWritten())
635     Out << " = delete";
636   else if (D->isExplicitlyDefaulted())
637     Out << " = default";
638   else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) {
639     if (!D->hasPrototype() && D->getNumParams()) {
640       // This is a K&R function definition, so we need to print the
641       // parameters.
642       Out << '\n';
643       DeclPrinter ParamPrinter(Out, SubPolicy, Indentation);
644       Indentation += Policy.Indentation;
645       for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
646         Indent();
647         ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
648         Out << ";\n";
649       }
650       Indentation -= Policy.Indentation;
651     } else
652       Out << ' ';
653 
654     if (D->getBody())
655       D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
656     Out << '\n';
657   }
658 }
659 
660 void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
661   if (TypeSourceInfo *TSI = D->getFriendType()) {
662     unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
663     for (unsigned i = 0; i < NumTPLists; ++i)
664       PrintTemplateParameters(D->getFriendTypeTemplateParameterList(i));
665     Out << "friend ";
666     Out << " " << TSI->getType().getAsString(Policy);
667   }
668   else if (FunctionDecl *FD =
669       dyn_cast<FunctionDecl>(D->getFriendDecl())) {
670     Out << "friend ";
671     VisitFunctionDecl(FD);
672   }
673   else if (FunctionTemplateDecl *FTD =
674            dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
675     Out << "friend ";
676     VisitFunctionTemplateDecl(FTD);
677   }
678   else if (ClassTemplateDecl *CTD =
679            dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
680     Out << "friend ";
681     VisitRedeclarableTemplateDecl(CTD);
682   }
683 }
684 
685 void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
686   // FIXME: add printing of pragma attributes if required.
687   if (!Policy.SuppressSpecifiers && D->isMutable())
688     Out << "mutable ";
689   if (!Policy.SuppressSpecifiers && D->isModulePrivate())
690     Out << "__module_private__ ";
691 
692   Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
693          stream(Policy, D->getName(), Indentation);
694 
695   if (D->isBitField()) {
696     Out << " : ";
697     D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
698   }
699 
700   Expr *Init = D->getInClassInitializer();
701   if (!Policy.SuppressInitializers && Init) {
702     if (D->getInClassInitStyle() == ICIS_ListInit)
703       Out << " ";
704     else
705       Out << " = ";
706     Init->printPretty(Out, nullptr, Policy, Indentation);
707   }
708   prettyPrintAttributes(D);
709 }
710 
711 void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
712   Out << *D << ":";
713 }
714 
715 void DeclPrinter::VisitVarDecl(VarDecl *D) {
716   prettyPrintPragmas(D);
717 
718   QualType T = D->getTypeSourceInfo()
719     ? D->getTypeSourceInfo()->getType()
720     : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
721 
722   if (!Policy.SuppressSpecifiers) {
723     StorageClass SC = D->getStorageClass();
724     if (SC != SC_None)
725       Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
726 
727     switch (D->getTSCSpec()) {
728     case TSCS_unspecified:
729       break;
730     case TSCS___thread:
731       Out << "__thread ";
732       break;
733     case TSCS__Thread_local:
734       Out << "_Thread_local ";
735       break;
736     case TSCS_thread_local:
737       Out << "thread_local ";
738       break;
739     }
740 
741     if (D->isModulePrivate())
742       Out << "__module_private__ ";
743 
744     if (D->isConstexpr()) {
745       Out << "constexpr ";
746       T.removeLocalConst();
747     }
748   }
749 
750   printDeclType(T, D->getName());
751   Expr *Init = D->getInit();
752   if (!Policy.SuppressInitializers && Init) {
753     bool ImplicitInit = false;
754     if (CXXConstructExpr *Construct =
755             dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
756       if (D->getInitStyle() == VarDecl::CallInit &&
757           !Construct->isListInitialization()) {
758         ImplicitInit = Construct->getNumArgs() == 0 ||
759           Construct->getArg(0)->isDefaultArgument();
760       }
761     }
762     if (!ImplicitInit) {
763       if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
764         Out << "(";
765       else if (D->getInitStyle() == VarDecl::CInit) {
766         Out << " = ";
767       }
768       PrintingPolicy SubPolicy(Policy);
769       SubPolicy.SuppressSpecifiers = false;
770       SubPolicy.IncludeTagDefinition = false;
771       Init->printPretty(Out, nullptr, SubPolicy, Indentation);
772       if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
773         Out << ")";
774     }
775   }
776   prettyPrintAttributes(D);
777 }
778 
779 void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
780   VisitVarDecl(D);
781 }
782 
783 void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
784   Out << "__asm (";
785   D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
786   Out << ")";
787 }
788 
789 void DeclPrinter::VisitImportDecl(ImportDecl *D) {
790   Out << "@import " << D->getImportedModule()->getFullModuleName()
791       << ";\n";
792 }
793 
794 void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
795   Out << "static_assert(";
796   D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
797   if (StringLiteral *SL = D->getMessage()) {
798     Out << ", ";
799     SL->printPretty(Out, nullptr, Policy, Indentation);
800   }
801   Out << ")";
802 }
803 
804 //----------------------------------------------------------------------------
805 // C++ declarations
806 //----------------------------------------------------------------------------
807 void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
808   if (D->isInline())
809     Out << "inline ";
810   Out << "namespace " << *D << " {\n";
811   VisitDeclContext(D);
812   Indent() << "}";
813 }
814 
815 void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
816   Out << "using namespace ";
817   if (D->getQualifier())
818     D->getQualifier()->print(Out, Policy);
819   Out << *D->getNominatedNamespaceAsWritten();
820 }
821 
822 void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
823   Out << "namespace " << *D << " = ";
824   if (D->getQualifier())
825     D->getQualifier()->print(Out, Policy);
826   Out << *D->getAliasedNamespace();
827 }
828 
829 void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
830   prettyPrintAttributes(D);
831 }
832 
833 void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
834   // FIXME: add printing of pragma attributes if required.
835   if (!Policy.SuppressSpecifiers && D->isModulePrivate())
836     Out << "__module_private__ ";
837   Out << D->getKindName();
838 
839   prettyPrintAttributes(D);
840 
841   if (D->getIdentifier())
842     Out << ' ' << *D;
843 
844   if (D->isCompleteDefinition()) {
845     // Print the base classes
846     if (D->getNumBases()) {
847       Out << " : ";
848       for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
849              BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
850         if (Base != D->bases_begin())
851           Out << ", ";
852 
853         if (Base->isVirtual())
854           Out << "virtual ";
855 
856         AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
857         if (AS != AS_none) {
858           Print(AS);
859           Out << " ";
860         }
861         Out << Base->getType().getAsString(Policy);
862 
863         if (Base->isPackExpansion())
864           Out << "...";
865       }
866     }
867 
868     // Print the class definition
869     // FIXME: Doesn't print access specifiers, e.g., "public:"
870     Out << " {\n";
871     VisitDeclContext(D);
872     Indent() << "}";
873   }
874 }
875 
876 void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
877   const char *l;
878   if (D->getLanguage() == LinkageSpecDecl::lang_c)
879     l = "C";
880   else {
881     assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
882            "unknown language in linkage specification");
883     l = "C++";
884   }
885 
886   Out << "extern \"" << l << "\" ";
887   if (D->hasBraces()) {
888     Out << "{\n";
889     VisitDeclContext(D);
890     Indent() << "}";
891   } else
892     Visit(*D->decls_begin());
893 }
894 
895 void DeclPrinter::PrintTemplateParameters(const TemplateParameterList *Params,
896                                           const TemplateArgumentList *Args) {
897   assert(Params);
898   assert(!Args || Params->size() == Args->size());
899 
900   Out << "template <";
901 
902   for (unsigned i = 0, e = Params->size(); i != e; ++i) {
903     if (i != 0)
904       Out << ", ";
905 
906     const Decl *Param = Params->getParam(i);
907     if (const TemplateTypeParmDecl *TTP =
908           dyn_cast<TemplateTypeParmDecl>(Param)) {
909 
910       if (TTP->wasDeclaredWithTypename())
911         Out << "typename ";
912       else
913         Out << "class ";
914 
915       if (TTP->isParameterPack())
916         Out << "...";
917 
918       Out << *TTP;
919 
920       if (Args) {
921         Out << " = ";
922         Args->get(i).print(Policy, Out);
923       } else if (TTP->hasDefaultArgument()) {
924         Out << " = ";
925         Out << TTP->getDefaultArgument().getAsString(Policy);
926       };
927     } else if (const NonTypeTemplateParmDecl *NTTP =
928                  dyn_cast<NonTypeTemplateParmDecl>(Param)) {
929       StringRef Name;
930       if (IdentifierInfo *II = NTTP->getIdentifier())
931         Name = II->getName();
932       printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
933 
934       if (Args) {
935         Out << " = ";
936         Args->get(i).print(Policy, Out);
937       } else if (NTTP->hasDefaultArgument()) {
938         Out << " = ";
939         NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
940                                                 Indentation);
941       }
942     } else if (const TemplateTemplateParmDecl *TTPD =
943                  dyn_cast<TemplateTemplateParmDecl>(Param)) {
944       VisitTemplateDecl(TTPD);
945       // FIXME: print the default argument, if present.
946     }
947   }
948 
949   Out << "> ";
950 }
951 
952 void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
953   PrintTemplateParameters(D->getTemplateParameters());
954 
955   if (const TemplateTemplateParmDecl *TTP =
956         dyn_cast<TemplateTemplateParmDecl>(D)) {
957     Out << "class ";
958     if (TTP->isParameterPack())
959       Out << "...";
960     Out << D->getName();
961   } else {
962     Visit(D->getTemplatedDecl());
963   }
964 }
965 
966 void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
967   if (PrintInstantiation) {
968     TemplateParameterList *Params = D->getTemplateParameters();
969     for (auto *I : D->specializations()) {
970       prettyPrintPragmas(I);
971       PrintTemplateParameters(Params, I->getTemplateSpecializationArgs());
972       Visit(I);
973     }
974   }
975 
976   prettyPrintPragmas(D->getTemplatedDecl());
977   return VisitRedeclarableTemplateDecl(D);
978 }
979 
980 void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
981   if (PrintInstantiation) {
982     TemplateParameterList *Params = D->getTemplateParameters();
983     for (auto *I : D->specializations()) {
984       PrintTemplateParameters(Params, &I->getTemplateArgs());
985       Visit(I);
986       Out << ";\n";
987     }
988   }
989 
990   return VisitRedeclarableTemplateDecl(D);
991 }
992 
993 //----------------------------------------------------------------------------
994 // Objective-C declarations
995 //----------------------------------------------------------------------------
996 
997 void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
998                                       Decl::ObjCDeclQualifier Quals,
999                                       QualType T) {
1000   Out << '(';
1001   if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
1002     Out << "in ";
1003   if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
1004     Out << "inout ";
1005   if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
1006     Out << "out ";
1007   if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
1008     Out << "bycopy ";
1009   if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
1010     Out << "byref ";
1011   if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
1012     Out << "oneway ";
1013   if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
1014     if (auto nullability = AttributedType::stripOuterNullability(T))
1015       Out << getNullabilitySpelling(*nullability, true) << ' ';
1016   }
1017 
1018   Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
1019   Out << ')';
1020 }
1021 
1022 void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1023   Out << "<";
1024   unsigned First = true;
1025   for (auto *Param : *Params) {
1026     if (First) {
1027       First = false;
1028     } else {
1029       Out << ", ";
1030     }
1031 
1032     switch (Param->getVariance()) {
1033     case ObjCTypeParamVariance::Invariant:
1034       break;
1035 
1036     case ObjCTypeParamVariance::Covariant:
1037       Out << "__covariant ";
1038       break;
1039 
1040     case ObjCTypeParamVariance::Contravariant:
1041       Out << "__contravariant ";
1042       break;
1043     }
1044 
1045     Out << Param->getDeclName().getAsString();
1046 
1047     if (Param->hasExplicitBound()) {
1048       Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1049     }
1050   }
1051   Out << ">";
1052 }
1053 
1054 void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1055   if (OMD->isInstanceMethod())
1056     Out << "- ";
1057   else
1058     Out << "+ ";
1059   if (!OMD->getReturnType().isNull()) {
1060     PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
1061                         OMD->getReturnType());
1062   }
1063 
1064   std::string name = OMD->getSelector().getAsString();
1065   std::string::size_type pos, lastPos = 0;
1066   for (const auto *PI : OMD->parameters()) {
1067     // FIXME: selector is missing here!
1068     pos = name.find_first_of(':', lastPos);
1069     Out << " " << name.substr(lastPos, pos - lastPos) << ':';
1070     PrintObjCMethodType(OMD->getASTContext(),
1071                         PI->getObjCDeclQualifier(),
1072                         PI->getType());
1073     Out << *PI;
1074     lastPos = pos + 1;
1075   }
1076 
1077   if (OMD->param_begin() == OMD->param_end())
1078     Out << " " << name;
1079 
1080   if (OMD->isVariadic())
1081       Out << ", ...";
1082 
1083   prettyPrintAttributes(OMD);
1084 
1085   if (OMD->getBody() && !Policy.TerseOutput) {
1086     Out << ' ';
1087     OMD->getBody()->printPretty(Out, nullptr, Policy);
1088   }
1089   else if (Policy.PolishForDeclaration)
1090     Out << ';';
1091 }
1092 
1093 void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1094   std::string I = OID->getNameAsString();
1095   ObjCInterfaceDecl *SID = OID->getSuperClass();
1096 
1097   bool eolnOut = false;
1098   if (SID)
1099     Out << "@implementation " << I << " : " << *SID;
1100   else
1101     Out << "@implementation " << I;
1102 
1103   if (OID->ivar_size() > 0) {
1104     Out << "{\n";
1105     eolnOut = true;
1106     Indentation += Policy.Indentation;
1107     for (const auto *I : OID->ivars()) {
1108       Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
1109                     getAsString(Policy) << ' ' << *I << ";\n";
1110     }
1111     Indentation -= Policy.Indentation;
1112     Out << "}\n";
1113   }
1114   else if (SID || (OID->decls_begin() != OID->decls_end())) {
1115     Out << "\n";
1116     eolnOut = true;
1117   }
1118   VisitDeclContext(OID, false);
1119   if (!eolnOut)
1120     Out << "\n";
1121   Out << "@end";
1122 }
1123 
1124 void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1125   std::string I = OID->getNameAsString();
1126   ObjCInterfaceDecl *SID = OID->getSuperClass();
1127 
1128   if (!OID->isThisDeclarationADefinition()) {
1129     Out << "@class " << I;
1130 
1131     if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1132       PrintObjCTypeParams(TypeParams);
1133     }
1134 
1135     Out << ";";
1136     return;
1137   }
1138   bool eolnOut = false;
1139   Out << "@interface " << I;
1140 
1141   if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1142     PrintObjCTypeParams(TypeParams);
1143   }
1144 
1145   if (SID)
1146     Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
1147 
1148   // Protocols?
1149   const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1150   if (!Protocols.empty()) {
1151     for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1152          E = Protocols.end(); I != E; ++I)
1153       Out << (I == Protocols.begin() ? '<' : ',') << **I;
1154     Out << "> ";
1155   }
1156 
1157   if (OID->ivar_size() > 0) {
1158     Out << "{\n";
1159     eolnOut = true;
1160     Indentation += Policy.Indentation;
1161     for (const auto *I : OID->ivars()) {
1162       Indent() << I->getASTContext()
1163                       .getUnqualifiedObjCPointerType(I->getType())
1164                       .getAsString(Policy) << ' ' << *I << ";\n";
1165     }
1166     Indentation -= Policy.Indentation;
1167     Out << "}\n";
1168   }
1169   else if (SID || (OID->decls_begin() != OID->decls_end())) {
1170     Out << "\n";
1171     eolnOut = true;
1172   }
1173 
1174   VisitDeclContext(OID, false);
1175   if (!eolnOut)
1176     Out << "\n";
1177   Out << "@end";
1178   // FIXME: implement the rest...
1179 }
1180 
1181 void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
1182   if (!PID->isThisDeclarationADefinition()) {
1183     Out << "@protocol " << *PID << ";\n";
1184     return;
1185   }
1186   // Protocols?
1187   const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1188   if (!Protocols.empty()) {
1189     Out << "@protocol " << *PID;
1190     for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1191          E = Protocols.end(); I != E; ++I)
1192       Out << (I == Protocols.begin() ? '<' : ',') << **I;
1193     Out << ">\n";
1194   } else
1195     Out << "@protocol " << *PID << '\n';
1196   VisitDeclContext(PID, false);
1197   Out << "@end";
1198 }
1199 
1200 void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
1201   Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
1202 
1203   VisitDeclContext(PID, false);
1204   Out << "@end";
1205   // FIXME: implement the rest...
1206 }
1207 
1208 void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
1209   Out << "@interface " << *PID->getClassInterface();
1210   if (auto TypeParams = PID->getTypeParamList()) {
1211     PrintObjCTypeParams(TypeParams);
1212   }
1213   Out << "(" << *PID << ")\n";
1214   if (PID->ivar_size() > 0) {
1215     Out << "{\n";
1216     Indentation += Policy.Indentation;
1217     for (const auto *I : PID->ivars())
1218       Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
1219                     getAsString(Policy) << ' ' << *I << ";\n";
1220     Indentation -= Policy.Indentation;
1221     Out << "}\n";
1222   }
1223 
1224   VisitDeclContext(PID, false);
1225   Out << "@end";
1226 
1227   // FIXME: implement the rest...
1228 }
1229 
1230 void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
1231   Out << "@compatibility_alias " << *AID
1232       << ' ' << *AID->getClassInterface() << ";\n";
1233 }
1234 
1235 /// PrintObjCPropertyDecl - print a property declaration.
1236 ///
1237 void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1238   if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1239     Out << "@required\n";
1240   else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1241     Out << "@optional\n";
1242 
1243   QualType T = PDecl->getType();
1244 
1245   Out << "@property";
1246   if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1247     bool first = true;
1248     Out << " (";
1249     if (PDecl->getPropertyAttributes() &
1250         ObjCPropertyDecl::OBJC_PR_readonly) {
1251       Out << (first ? ' ' : ',') << "readonly";
1252       first = false;
1253     }
1254 
1255     if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1256       Out << (first ? ' ' : ',') << "getter = ";
1257       PDecl->getGetterName().print(Out);
1258       first = false;
1259     }
1260     if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1261       Out << (first ? ' ' : ',') << "setter = ";
1262       PDecl->getSetterName().print(Out);
1263       first = false;
1264     }
1265 
1266     if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1267       Out << (first ? ' ' : ',') << "assign";
1268       first = false;
1269     }
1270 
1271     if (PDecl->getPropertyAttributes() &
1272         ObjCPropertyDecl::OBJC_PR_readwrite) {
1273       Out << (first ? ' ' : ',') << "readwrite";
1274       first = false;
1275     }
1276 
1277     if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1278       Out << (first ? ' ' : ',') << "retain";
1279       first = false;
1280     }
1281 
1282     if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1283       Out << (first ? ' ' : ',') << "strong";
1284       first = false;
1285     }
1286 
1287     if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1288       Out << (first ? ' ' : ',') << "copy";
1289       first = false;
1290     }
1291 
1292     if (PDecl->getPropertyAttributes() &
1293         ObjCPropertyDecl::OBJC_PR_nonatomic) {
1294       Out << (first ? ' ' : ',') << "nonatomic";
1295       first = false;
1296     }
1297     if (PDecl->getPropertyAttributes() &
1298         ObjCPropertyDecl::OBJC_PR_atomic) {
1299       Out << (first ? ' ' : ',') << "atomic";
1300       first = false;
1301     }
1302 
1303     if (PDecl->getPropertyAttributes() &
1304         ObjCPropertyDecl::OBJC_PR_nullability) {
1305       if (auto nullability = AttributedType::stripOuterNullability(T)) {
1306         if (*nullability == NullabilityKind::Unspecified &&
1307             (PDecl->getPropertyAttributes() &
1308                ObjCPropertyDecl::OBJC_PR_null_resettable)) {
1309           Out << (first ? ' ' : ',') << "null_resettable";
1310         } else {
1311           Out << (first ? ' ' : ',')
1312               << getNullabilitySpelling(*nullability, true);
1313         }
1314         first = false;
1315       }
1316     }
1317 
1318     if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) {
1319       Out << (first ? ' ' : ',') << "class";
1320       first = false;
1321     }
1322 
1323     (void) first; // Silence dead store warning due to idiomatic code.
1324     Out << " )";
1325   }
1326   Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
1327                   getAsString(Policy) << ' ' << *PDecl;
1328   if (Policy.PolishForDeclaration)
1329     Out << ';';
1330 }
1331 
1332 void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1333   if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1334     Out << "@synthesize ";
1335   else
1336     Out << "@dynamic ";
1337   Out << *PID->getPropertyDecl();
1338   if (PID->getPropertyIvarDecl())
1339     Out << '=' << *PID->getPropertyIvarDecl();
1340 }
1341 
1342 void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
1343   if (!D->isAccessDeclaration())
1344     Out << "using ";
1345   if (D->hasTypename())
1346     Out << "typename ";
1347   D->getQualifier()->print(Out, Policy);
1348 
1349   // Use the correct record name when the using declaration is used for
1350   // inheriting constructors.
1351   for (const auto *Shadow : D->shadows()) {
1352     if (const auto *ConstructorShadow =
1353             dyn_cast<ConstructorUsingShadowDecl>(Shadow)) {
1354       assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext());
1355       Out << *ConstructorShadow->getNominatedBaseClass();
1356       return;
1357     }
1358   }
1359   Out << *D;
1360 }
1361 
1362 void
1363 DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1364   Out << "using typename ";
1365   D->getQualifier()->print(Out, Policy);
1366   Out << D->getDeclName();
1367 }
1368 
1369 void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1370   if (!D->isAccessDeclaration())
1371     Out << "using ";
1372   D->getQualifier()->print(Out, Policy);
1373   Out << D->getDeclName();
1374 }
1375 
1376 void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1377   // ignore
1378 }
1379 
1380 void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1381   Out << "#pragma omp threadprivate";
1382   if (!D->varlist_empty()) {
1383     for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1384                                                 E = D->varlist_end();
1385                                                 I != E; ++I) {
1386       Out << (I == D->varlist_begin() ? '(' : ',');
1387       NamedDecl *ND = cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
1388       ND->printQualifiedName(Out);
1389     }
1390     Out << ")";
1391   }
1392 }
1393 
1394 void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1395   if (!D->isInvalidDecl()) {
1396     Out << "#pragma omp declare reduction (";
1397     if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) {
1398       static const char *const OperatorNames[NUM_OVERLOADED_OPERATORS] = {
1399           nullptr,
1400 #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly)  \
1401           Spelling,
1402 #include "clang/Basic/OperatorKinds.def"
1403       };
1404       const char *OpName =
1405           OperatorNames[D->getDeclName().getCXXOverloadedOperator()];
1406       assert(OpName && "not an overloaded operator");
1407       Out << OpName;
1408     } else {
1409       assert(D->getDeclName().isIdentifier());
1410       D->printName(Out);
1411     }
1412     Out << " : ";
1413     D->getType().print(Out, Policy);
1414     Out << " : ";
1415     D->getCombiner()->printPretty(Out, nullptr, Policy, 0);
1416     Out << ")";
1417     if (auto *Init = D->getInitializer()) {
1418       Out << " initializer(";
1419       Init->printPretty(Out, nullptr, Policy, 0);
1420       Out << ")";
1421     }
1422   }
1423 }
1424 
1425 void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
1426   D->getInit()->printPretty(Out, nullptr, Policy, Indentation);
1427 }
1428 
1429