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