Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/DeclPrinter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file implements the Decl::print method, which pretty prints the
10
// AST back out to C/Objective-C/C++/Objective-C++ code.
11
//
12
//===----------------------------------------------------------------------===//
13
#include "clang/AST/ASTContext.h"
14
#include "clang/AST/Attr.h"
15
#include "clang/AST/Decl.h"
16
#include "clang/AST/DeclCXX.h"
17
#include "clang/AST/DeclObjC.h"
18
#include "clang/AST/DeclTemplate.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
    const ASTContext &Context;
32
    unsigned Indentation;
33
    bool PrintInstantiation;
34
35
17.2k
    raw_ostream& Indent() { return Indent(Indentation); }
36
    raw_ostream& Indent(unsigned Indentation);
37
    void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls);
38
39
    void Print(AccessSpecifier AS);
40
    void PrintConstructorInitializers(CXXConstructorDecl *CDecl,
41
                                      std::string &Proto);
42
43
    /// Print an Objective-C method type in parentheses.
44
    ///
45
    /// \param Quals The Objective-C declaration qualifiers.
46
    /// \param T The type to print.
47
    void PrintObjCMethodType(ASTContext &Ctx, Decl::ObjCDeclQualifier Quals,
48
                             QualType T);
49
50
    void PrintObjCTypeParams(ObjCTypeParamList *Params);
51
52
  public:
53
    DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy,
54
                const ASTContext &Context, unsigned Indentation = 0,
55
                bool PrintInstantiation = false)
56
        : Out(Out), Policy(Policy), Context(Context), Indentation(Indentation),
57
32.4k
          PrintInstantiation(PrintInstantiation) {}
58
59
    void VisitDeclContext(DeclContext *DC, bool Indent = true);
60
61
    void VisitTranslationUnitDecl(TranslationUnitDecl *D);
62
    void VisitTypedefDecl(TypedefDecl *D);
63
    void VisitTypeAliasDecl(TypeAliasDecl *D);
64
    void VisitEnumDecl(EnumDecl *D);
65
    void VisitRecordDecl(RecordDecl *D);
66
    void VisitEnumConstantDecl(EnumConstantDecl *D);
67
    void VisitEmptyDecl(EmptyDecl *D);
68
    void VisitFunctionDecl(FunctionDecl *D);
69
    void VisitFriendDecl(FriendDecl *D);
70
    void VisitFieldDecl(FieldDecl *D);
71
    void VisitVarDecl(VarDecl *D);
72
    void VisitLabelDecl(LabelDecl *D);
73
    void VisitParmVarDecl(ParmVarDecl *D);
74
    void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
75
    void VisitImportDecl(ImportDecl *D);
76
    void VisitStaticAssertDecl(StaticAssertDecl *D);
77
    void VisitNamespaceDecl(NamespaceDecl *D);
78
    void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
79
    void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
80
    void VisitCXXRecordDecl(CXXRecordDecl *D);
81
    void VisitLinkageSpecDecl(LinkageSpecDecl *D);
82
    void VisitTemplateDecl(const TemplateDecl *D);
83
    void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
84
    void VisitClassTemplateDecl(ClassTemplateDecl *D);
85
    void VisitClassTemplateSpecializationDecl(
86
                                            ClassTemplateSpecializationDecl *D);
87
    void VisitClassTemplatePartialSpecializationDecl(
88
                                     ClassTemplatePartialSpecializationDecl *D);
89
    void VisitObjCMethodDecl(ObjCMethodDecl *D);
90
    void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
91
    void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
92
    void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
93
    void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
94
    void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
95
    void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
96
    void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
97
    void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
98
    void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
99
    void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
100
    void VisitUsingDecl(UsingDecl *D);
101
    void VisitUsingEnumDecl(UsingEnumDecl *D);
102
    void VisitUsingShadowDecl(UsingShadowDecl *D);
103
    void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
104
    void VisitOMPAllocateDecl(OMPAllocateDecl *D);
105
    void VisitOMPRequiresDecl(OMPRequiresDecl *D);
106
    void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
107
    void VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D);
108
    void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
109
    void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *TTP);
110
    void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *NTTP);
111
112
    void printTemplateParameters(const TemplateParameterList *Params,
113
                                 bool OmitTemplateKW = false);
114
    void printTemplateArguments(llvm::ArrayRef<TemplateArgument> Args,
115
                                const TemplateParameterList *Params);
116
    void printTemplateArguments(llvm::ArrayRef<TemplateArgumentLoc> Args,
117
                                const TemplateParameterList *Params);
118
    void prettyPrintAttributes(Decl *D);
119
    void prettyPrintPragmas(Decl *D);
120
    void printDeclType(QualType T, StringRef DeclName, bool Pack = false);
121
  };
122
}
123
124
void Decl::print(raw_ostream &Out, unsigned Indentation,
125
491
                 bool PrintInstantiation) const {
126
491
  print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
127
491
}
128
129
void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
130
24.2k
                 unsigned Indentation, bool PrintInstantiation) const {
131
24.2k
  DeclPrinter Printer(Out, Policy, getASTContext(), Indentation,
132
24.2k
                      PrintInstantiation);
133
24.2k
  Printer.Visit(const_cast<Decl*>(this));
134
24.2k
}
135
136
void TemplateParameterList::print(raw_ostream &Out, const ASTContext &Context,
137
1
                                  bool OmitTemplateKW) const {
138
1
  print(Out, Context, Context.getPrintingPolicy(), OmitTemplateKW);
139
1
}
140
141
void TemplateParameterList::print(raw_ostream &Out, const ASTContext &Context,
142
                                  const PrintingPolicy &Policy,
143
1
                                  bool OmitTemplateKW) const {
144
1
  DeclPrinter Printer(Out, Policy, Context);
145
1
  Printer.printTemplateParameters(this, OmitTemplateKW);
146
1
}
147
148
223
static QualType GetBaseType(QualType T) {
149
  // FIXME: This should be on the Type class!
150
223
  QualType BaseType = T;
151
308
  while (!BaseType->isSpecifierType()) {
152
86
    if (const PointerType *PTy = BaseType->getAs<PointerType>())
153
42
      BaseType = PTy->getPointeeType();
154
44
    else if (const ObjCObjectPointerType *OPT =
155
44
                 BaseType->getAs<ObjCObjectPointerType>())
156
0
      BaseType = OPT->getPointeeType();
157
44
    else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
158
0
      BaseType = BPy->getPointeeType();
159
44
    else if (const ArrayType *ATy = dyn_cast<ArrayType>(BaseType))
160
8
      BaseType = ATy->getElementType();
161
36
    else if (const FunctionType *FTy = BaseType->getAs<FunctionType>())
162
33
      BaseType = FTy->getReturnType();
163
3
    else if (const VectorType *VTy = BaseType->getAs<VectorType>())
164
0
      BaseType = VTy->getElementType();
165
3
    else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
166
0
      BaseType = RTy->getPointeeType();
167
3
    else if (const AutoType *ATy = BaseType->getAs<AutoType>())
168
1
      BaseType = ATy->getDeducedType();
169
2
    else if (const ParenType *PTy = BaseType->getAs<ParenType>())
170
1
      BaseType = PTy->desugar();
171
1
    else
172
      // This must be a syntax error.
173
1
      break;
174
86
  }
175
223
  return BaseType;
176
223
}
177
178
13.5k
static QualType getDeclType(Decl* D) {
179
13.5k
  if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
180
440
    return TDD->getUnderlyingType();
181
13.1k
  if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
182
9.11k
    return VD->getType();
183
4.01k
  return QualType();
184
13.1k
}
185
186
void Decl::printGroup(Decl** Begin, unsigned NumDecls,
187
                      raw_ostream &Out, const PrintingPolicy &Policy,
188
14.3k
                      unsigned Indentation) {
189
14.3k
  if (NumDecls == 1) {
190
12.8k
    (*Begin)->print(Out, Policy, Indentation);
191
12.8k
    return;
192
12.8k
  }
193
194
1.52k
  Decl** End = Begin + NumDecls;
195
1.52k
  TagDecl* TD = dyn_cast<TagDecl>(*Begin);
196
1.52k
  if (TD)
197
248
    ++Begin;
198
199
1.52k
  PrintingPolicy SubPolicy(Policy);
200
201
1.52k
  bool isFirst = true;
202
8.04k
  for ( ; Begin != End; 
++Begin6.52k
) {
203
6.52k
    if (isFirst) {
204
1.52k
      if(TD)
205
248
        SubPolicy.IncludeTagDefinition = true;
206
1.52k
      SubPolicy.SuppressSpecifiers = false;
207
1.52k
      isFirst = false;
208
5.00k
    } else {
209
5.00k
      if (!isFirst) Out << ", ";
210
5.00k
      SubPolicy.IncludeTagDefinition = false;
211
5.00k
      SubPolicy.SuppressSpecifiers = true;
212
5.00k
    }
213
214
6.52k
    (*Begin)->print(Out, SubPolicy, Indentation);
215
6.52k
  }
216
1.52k
}
217
218
0
LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const {
219
  // Get the translation unit
220
0
  const DeclContext *DC = this;
221
0
  while (!DC->isTranslationUnit())
222
0
    DC = DC->getParent();
223
224
0
  ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
225
0
  DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), Ctx, 0);
226
0
  Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
227
0
}
228
229
17.2k
raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
230
32.1k
  for (unsigned i = 0; i != Indentation; 
++i14.8k
)
231
14.8k
    Out << "  ";
232
17.2k
  return Out;
233
17.2k
}
234
235
40.9k
void DeclPrinter::prettyPrintAttributes(Decl *D) {
236
40.9k
  if (Policy.PolishForDeclaration)
237
2.10k
    return;
238
239
38.8k
  if (D->hasAttrs()) {
240
1.45k
    AttrVec &Attrs = D->getAttrs();
241
1.82k
    for (auto *A : Attrs) {
242
1.82k
      if (A->isInherited() || 
A->isImplicit()1.64k
)
243
1.35k
        continue;
244
470
      switch (A->getKind()) {
245
0
#define ATTR(X)
246
0
#define PRAGMA_SPELLING_ATTR(X) case attr::X:
247
0
#include "clang/Basic/AttrList.inc"
248
0
        break;
249
470
      default:
250
470
        A->printPretty(Out, Policy);
251
470
        break;
252
470
      }
253
470
    }
254
1.45k
  }
255
38.8k
}
256
257
33.8k
void DeclPrinter::prettyPrintPragmas(Decl *D) {
258
33.8k
  if (Policy.PolishForDeclaration)
259
1.75k
    return;
260
261
32.0k
  if (D->hasAttrs()) {
262
1.25k
    AttrVec &Attrs = D->getAttrs();
263
1.51k
    for (auto *A : Attrs) {
264
1.51k
      switch (A->getKind()) {
265
0
#define ATTR(X)
266
1.06k
#define PRAGMA_SPELLING_ATTR(X) case attr::X:
267
550
#include 
"clang/Basic/AttrList.inc"0
268
550
        A->printPretty(Out, Policy);
269
550
        Indent();
270
550
        break;
271
966
      default:
272
966
        break;
273
1.51k
      }
274
1.51k
    }
275
1.25k
  }
276
32.0k
}
277
278
26.5k
void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) {
279
  // Normally, a PackExpansionType is written as T[3]... (for instance, as a
280
  // template argument), but if it is the type of a declaration, the ellipsis
281
  // is placed before the name being declared.
282
26.5k
  if (auto *PET = T->getAs<PackExpansionType>()) {
283
14
    Pack = true;
284
14
    T = PET->getPattern();
285
14
  }
286
26.5k
  T.print(Out, Policy, (Pack ? 
"..."22
:
""26.5k
) + DeclName, Indentation);
287
26.5k
}
288
289
189
void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
290
189
  this->Indent();
291
189
  Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
292
189
  Out << ";\n";
293
189
  Decls.clear();
294
295
189
}
296
297
1.46k
void DeclPrinter::Print(AccessSpecifier AS) {
298
1.46k
  const auto AccessSpelling = getAccessSpelling(AS);
299
1.46k
  if (AccessSpelling.empty())
300
0
    llvm_unreachable("No access specifier!");
301
1.46k
  Out << AccessSpelling;
302
1.46k
}
303
304
void DeclPrinter::PrintConstructorInitializers(CXXConstructorDecl *CDecl,
305
1.70k
                                               std::string &Proto) {
306
1.70k
  bool HasInitializerList = false;
307
2.03k
  for (const auto *BMInitializer : CDecl->inits()) {
308
2.03k
    if (BMInitializer->isInClassMemberInitializer())
309
0
      continue;
310
311
2.03k
    if (!HasInitializerList) {
312
1.54k
      Proto += " : ";
313
1.54k
      Out << Proto;
314
1.54k
      Proto.clear();
315
1.54k
      HasInitializerList = true;
316
1.54k
    } else
317
488
      Out << ", ";
318
319
2.03k
    if (BMInitializer->isAnyMemberInitializer()) {
320
1.45k
      FieldDecl *FD = BMInitializer->getAnyMember();
321
1.45k
      Out << *FD;
322
1.45k
    } else {
323
580
      Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
324
580
    }
325
326
2.03k
    Out << "(";
327
2.03k
    if (!BMInitializer->getInit()) {
328
      // Nothing to print
329
2.03k
    } else {
330
2.03k
      Expr *Init = BMInitializer->getInit();
331
2.03k
      if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
332
0
        Init = Tmp->getSubExpr();
333
334
2.03k
      Init = Init->IgnoreParens();
335
336
2.03k
      Expr *SimpleInit = nullptr;
337
2.03k
      Expr **Args = nullptr;
338
2.03k
      unsigned NumArgs = 0;
339
2.03k
      if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
340
348
        Args = ParenList->getExprs();
341
348
        NumArgs = ParenList->getNumExprs();
342
1.68k
      } else if (CXXConstructExpr *Construct =
343
1.68k
                     dyn_cast<CXXConstructExpr>(Init)) {
344
998
        Args = Construct->getArgs();
345
998
        NumArgs = Construct->getNumArgs();
346
998
      } else
347
687
        SimpleInit = Init;
348
349
2.03k
      if (SimpleInit)
350
687
        SimpleInit->printPretty(Out, nullptr, Policy, Indentation, "\n",
351
687
                                &Context);
352
1.34k
      else {
353
2.11k
        for (unsigned I = 0; I != NumArgs; 
++I764
) {
354
764
          assert(Args[I] != nullptr && "Expected non-null Expr");
355
764
          if (isa<CXXDefaultArgExpr>(Args[I]))
356
0
            break;
357
358
764
          if (I)
359
0
            Out << ", ";
360
764
          Args[I]->printPretty(Out, nullptr, Policy, Indentation, "\n",
361
764
                               &Context);
362
764
        }
363
1.34k
      }
364
2.03k
    }
365
2.03k
    Out << ")";
366
2.03k
    if (BMInitializer->isPackExpansion())
367
0
      Out << "...";
368
2.03k
  }
369
1.70k
}
370
371
//----------------------------------------------------------------------------
372
// Common C declarations
373
//----------------------------------------------------------------------------
374
375
3.23k
void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
376
3.23k
  if (Policy.TerseOutput)
377
58
    return;
378
379
3.17k
  if (Indent)
380
2.23k
    Indentation += Policy.Indentation;
381
382
3.17k
  SmallVector<Decl*, 2> Decls;
383
3.17k
  for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
384
25.9k
       D != DEnd; 
++D22.7k
) {
385
386
    // Don't print ObjCIvarDecls, as they are printed when visiting the
387
    // containing ObjCInterfaceDecl.
388
22.7k
    if (isa<ObjCIvarDecl>(*D))
389
46
      continue;
390
391
    // Skip over implicit declarations in pretty-printing mode.
392
22.7k
    if (D->isImplicit())
393
9.17k
      continue;
394
395
    // Don't print implicit specializations, as they are printed when visiting
396
    // corresponding templates.
397
13.5k
    if (auto FD = dyn_cast<FunctionDecl>(*D))
398
4.72k
      if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation &&
399
4.72k
          
!isa<ClassTemplateSpecializationDecl>(DC)694
)
400
0
        continue;
401
402
    // The next bits of code handle stuff like "struct {int x;} a,b"; we're
403
    // forced to merge the declarations because there's no other way to
404
    // refer to the struct in question.  When that struct is named instead, we
405
    // also need to merge to avoid splitting off a stand-alone struct
406
    // declaration that produces the warning ext_no_declarators in some
407
    // contexts.
408
    //
409
    // This limited merging is safe without a bunch of other checks because it
410
    // only merges declarations directly referring to the tag, not typedefs.
411
    //
412
    // Check whether the current declaration should be grouped with a previous
413
    // non-free-standing tag declaration.
414
13.5k
    QualType CurDeclType = getDeclType(*D);
415
13.5k
    if (!Decls.empty() && 
!CurDeclType.isNull()287
) {
416
223
      QualType BaseType = GetBaseType(CurDeclType);
417
223
      if (!BaseType.isNull() && isa<ElaboratedType>(BaseType) &&
418
223
          
cast<ElaboratedType>(BaseType)->getOwnedTagDecl() == Decls[0]154
) {
419
146
        Decls.push_back(*D);
420
146
        continue;
421
146
      }
422
223
    }
423
424
    // If we have a merged group waiting to be handled, handle it now.
425
13.4k
    if (!Decls.empty())
426
141
      ProcessDeclGroup(Decls);
427
428
    // If the current declaration is not a free standing declaration, save it
429
    // so we can merge it with the subsequent declaration(s) using it.
430
13.4k
    if (isa<TagDecl>(*D) && 
!cast<TagDecl>(*D)->isFreeStanding()1.13k
) {
431
189
      Decls.push_back(*D);
432
189
      continue;
433
189
    }
434
435
13.2k
    if (isa<AccessSpecDecl>(*D)) {
436
983
      Indentation -= Policy.Indentation;
437
983
      this->Indent();
438
983
      Print(D->getAccess());
439
983
      Out << ":\n";
440
983
      Indentation += Policy.Indentation;
441
983
      continue;
442
983
    }
443
444
12.2k
    this->Indent();
445
12.2k
    Visit(*D);
446
447
    // FIXME: Need to be able to tell the DeclPrinter when
448
12.2k
    const char *Terminator = nullptr;
449
12.2k
    if (isa<OMPThreadPrivateDecl>(*D) || 
isa<OMPDeclareReductionDecl>(*D)12.0k
||
450
12.2k
        
isa<OMPDeclareMapperDecl>(*D)11.9k
||
isa<OMPRequiresDecl>(*D)11.9k
||
451
12.2k
        
isa<OMPAllocateDecl>(*D)11.8k
)
452
484
      Terminator = nullptr;
453
11.7k
    else if (isa<ObjCMethodDecl>(*D) && 
cast<ObjCMethodDecl>(*D)->hasBody()154
)
454
9
      Terminator = nullptr;
455
11.7k
    else if (auto FD = dyn_cast<FunctionDecl>(*D)) {
456
4.72k
      if (FD->isThisDeclarationADefinition())
457
3.88k
        Terminator = nullptr;
458
837
      else
459
837
        Terminator = ";";
460
7.03k
    } else if (auto TD = dyn_cast<FunctionTemplateDecl>(*D)) {
461
589
      if (TD->getTemplatedDecl()->isThisDeclarationADefinition())
462
504
        Terminator = nullptr;
463
85
      else
464
85
        Terminator = ";";
465
6.44k
    } else if (isa<NamespaceDecl>(*D) || 
isa<LinkageSpecDecl>(*D)6.32k
||
466
6.44k
             
isa<ObjCImplementationDecl>(*D)6.29k
||
467
6.44k
             
isa<ObjCInterfaceDecl>(*D)6.28k
||
468
6.44k
             
isa<ObjCProtocolDecl>(*D)6.25k
||
469
6.44k
             
isa<ObjCCategoryImplDecl>(*D)6.24k
||
470
6.44k
             
isa<ObjCCategoryDecl>(*D)6.24k
)
471
216
      Terminator = nullptr;
472
6.23k
    else if (isa<EnumConstantDecl>(*D)) {
473
237
      DeclContext::decl_iterator Next = D;
474
237
      ++Next;
475
237
      if (Next != DEnd)
476
134
        Terminator = ",";
477
237
    } else
478
5.99k
      Terminator = ";";
479
480
12.2k
    if (Terminator)
481
7.05k
      Out << Terminator;
482
12.2k
    if (!Policy.TerseOutput &&
483
12.2k
        ((isa<FunctionDecl>(*D) &&
484
12.2k
          
cast<FunctionDecl>(*D)->doesThisDeclarationHaveABody()4.72k
) ||
485
12.2k
         
(8.36k
isa<FunctionTemplateDecl>(*D)8.36k
&&
486
8.36k
          
cast<FunctionTemplateDecl>(*D)->getTemplatedDecl()->doesThisDeclarationHaveABody()589
)))
487
4.38k
      ; // StmtPrinter already added '\n' after CompoundStmt.
488
7.86k
    else
489
7.86k
      Out << "\n";
490
491
    // Declare target attribute is special one, natural spelling for the pragma
492
    // assumes "ending" construct so print it here.
493
12.2k
    if (D->hasAttr<OMPDeclareTargetDeclAttr>())
494
286
      Out << "#pragma omp end declare target\n";
495
12.2k
  }
496
497
3.17k
  if (!Decls.empty())
498
48
    ProcessDeclGroup(Decls);
499
500
3.17k
  if (Indent)
501
2.23k
    Indentation -= Policy.Indentation;
502
3.17k
}
503
504
862
void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
505
862
  VisitDeclContext(D, false);
506
862
}
507
508
493
void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
509
493
  if (!Policy.SuppressSpecifiers) {
510
493
    Out << "typedef ";
511
512
493
    if (D->isModulePrivate())
513
0
      Out << "__module_private__ ";
514
493
  }
515
493
  QualType Ty = D->getTypeSourceInfo()->getType();
516
493
  Ty.print(Out, Policy, D->getName(), Indentation);
517
493
  prettyPrintAttributes(D);
518
493
}
519
520
56
void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
521
56
  Out << "using " << *D;
522
56
  prettyPrintAttributes(D);
523
56
  Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
524
56
}
525
526
161
void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
527
161
  if (!Policy.SuppressSpecifiers && D->isModulePrivate())
528
0
    Out << "__module_private__ ";
529
161
  Out << "enum";
530
161
  if (D->isScoped()) {
531
35
    if (D->isScopedUsingClassTag())
532
35
      Out << " class";
533
0
    else
534
0
      Out << " struct";
535
35
  }
536
537
161
  prettyPrintAttributes(D);
538
539
161
  if (D->getDeclName())
540
139
    Out << ' ' << D->getDeclName();
541
542
161
  if (D->isFixed())
543
15
    Out << " : " << D->getIntegerType().stream(Policy);
544
545
161
  if (D->isCompleteDefinition()) {
546
152
    Out << " {\n";
547
152
    VisitDeclContext(D);
548
152
    Indent() << "}";
549
152
  }
550
161
}
551
552
169
void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
553
169
  if (!Policy.SuppressSpecifiers && D->isModulePrivate())
554
0
    Out << "__module_private__ ";
555
169
  Out << D->getKindName();
556
557
169
  prettyPrintAttributes(D);
558
559
169
  if (D->getIdentifier())
560
154
    Out << ' ' << *D;
561
562
169
  if (D->isCompleteDefinition()) {
563
133
    Out << " {\n";
564
133
    VisitDeclContext(D);
565
133
    Indent() << "}";
566
133
  }
567
169
}
568
569
263
void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
570
263
  Out << *D;
571
263
  prettyPrintAttributes(D);
572
263
  if (Expr *Init = D->getInitExpr()) {
573
31
    Out << " = ";
574
31
    Init->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context);
575
31
  }
576
263
}
577
578
static void printExplicitSpecifier(ExplicitSpecifier ES, llvm::raw_ostream &Out,
579
                                   PrintingPolicy &Policy, unsigned Indentation,
580
59
                                   const ASTContext &Context) {
581
59
  std::string Proto = "explicit";
582
59
  llvm::raw_string_ostream EOut(Proto);
583
59
  if (ES.getExpr()) {
584
54
    EOut << "(";
585
54
    ES.getExpr()->printPretty(EOut, nullptr, Policy, Indentation, "\n",
586
54
                              &Context);
587
54
    EOut << ")";
588
54
  }
589
59
  EOut << " ";
590
59
  EOut.flush();
591
59
  Out << EOut.str();
592
59
}
593
594
7.70k
void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
595
7.70k
  if (!D->getDescribedFunctionTemplate() &&
596
7.70k
      
!D->isFunctionTemplateSpecialization()7.04k
)
597
6.36k
    prettyPrintPragmas(D);
598
599
7.70k
  if (D->isFunctionTemplateSpecialization())
600
678
    Out << "template<> ";
601
7.03k
  else if (!D->getDescribedFunctionTemplate()) {
602
6.36k
    for (unsigned I = 0, NumTemplateParams = D->getNumTemplateParameterLists();
603
6.37k
         I < NumTemplateParams; 
++I6
)
604
6
      printTemplateParameters(D->getTemplateParameterList(I));
605
6.36k
  }
606
607
7.70k
  CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
608
7.70k
  CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
609
7.70k
  CXXDeductionGuideDecl *GuideDecl = dyn_cast<CXXDeductionGuideDecl>(D);
610
7.70k
  if (!Policy.SuppressSpecifiers) {
611
7.70k
    switch (D->getStorageClass()) {
612
7.57k
    case SC_None: break;
613
21
    case SC_Extern: Out << "extern "; break;
614
108
    case SC_Static: Out << "static "; break;
615
0
    case SC_PrivateExtern: Out << "__private_extern__ "; break;
616
0
    case SC_Auto: case SC_Register:
617
0
      llvm_unreachable("invalid for functions");
618
7.70k
    }
619
620
7.70k
    if (D->isInlineSpecified())  
Out << "inline "117
;
621
7.70k
    if (D->isVirtualAsWritten()) 
Out << "virtual "11
;
622
7.70k
    if (D->isModulePrivate())    
Out << "__module_private__ "0
;
623
7.70k
    if (D->isConstexprSpecified() && 
!D->isExplicitlyDefaulted()33
)
624
28
      Out << "constexpr ";
625
7.70k
    if (D->isConsteval())        
Out << "consteval "0
;
626
7.70k
    ExplicitSpecifier ExplicitSpec = ExplicitSpecifier::getFromDecl(D);
627
7.70k
    if (ExplicitSpec.isSpecified())
628
59
      printExplicitSpecifier(ExplicitSpec, Out, Policy, Indentation, Context);
629
7.70k
  }
630
631
7.70k
  PrintingPolicy SubPolicy(Policy);
632
7.70k
  SubPolicy.SuppressSpecifiers = false;
633
7.70k
  std::string Proto;
634
635
7.70k
  if (Policy.FullyQualifiedName) {
636
5
    Proto += D->getQualifiedNameAsString();
637
7.70k
  } else {
638
7.70k
    llvm::raw_string_ostream OS(Proto);
639
7.70k
    if (!Policy.SuppressScope) {
640
7.48k
      if (const NestedNameSpecifier *NS = D->getQualifier()) {
641
62
        NS->print(OS, Policy);
642
62
      }
643
7.48k
    }
644
7.70k
    D->getNameInfo().printName(OS, Policy);
645
7.70k
  }
646
647
7.70k
  if (GuideDecl)
648
7
    Proto = GuideDecl->getDeducedTemplate()->getDeclName().getAsString();
649
7.70k
  if (D->isFunctionTemplateSpecialization()) {
650
678
    llvm::raw_string_ostream POut(Proto);
651
678
    DeclPrinter TArgPrinter(POut, SubPolicy, Context, Indentation);
652
678
    const auto *TArgAsWritten = D->getTemplateSpecializationArgsAsWritten();
653
678
    if (TArgAsWritten && 
!Policy.PrintCanonicalTypes19
)
654
19
      TArgPrinter.printTemplateArguments(TArgAsWritten->arguments(), nullptr);
655
659
    else if (const TemplateArgumentList *TArgs =
656
659
                 D->getTemplateSpecializationArgs())
657
659
      TArgPrinter.printTemplateArguments(TArgs->asArray(), nullptr);
658
678
  }
659
660
7.70k
  QualType Ty = D->getType();
661
7.70k
  while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
662
0
    Proto = '(' + Proto + ')';
663
0
    Ty = PT->getInnerType();
664
0
  }
665
666
7.70k
  if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
667
7.70k
    const FunctionProtoType *FT = nullptr;
668
7.70k
    if (D->hasWrittenPrototype())
669
7.47k
      FT = dyn_cast<FunctionProtoType>(AFT);
670
671
7.70k
    Proto += "(";
672
7.70k
    if (FT) {
673
7.47k
      llvm::raw_string_ostream POut(Proto);
674
7.47k
      DeclPrinter ParamPrinter(POut, SubPolicy, Context, Indentation);
675
13.0k
      for (unsigned i = 0, e = D->getNumParams(); i != e; 
++i5.54k
) {
676
5.54k
        if (i) 
POut << ", "1.53k
;
677
5.54k
        ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
678
5.54k
      }
679
680
7.47k
      if (FT->isVariadic()) {
681
54
        if (D->getNumParams()) 
POut << ", "47
;
682
54
        POut << "...";
683
54
      }
684
7.47k
    } else 
if (232
D->doesThisDeclarationHaveABody()232
&&
!D->hasPrototype()179
) {
685
185
      for (unsigned i = 0, e = D->getNumParams(); i != e; 
++i6
) {
686
6
        if (i)
687
1
          Proto += ", ";
688
6
        Proto += D->getParamDecl(i)->getNameAsString();
689
6
      }
690
179
    }
691
692
7.70k
    Proto += ")";
693
694
7.70k
    if (FT) {
695
7.47k
      if (FT->isConst())
696
54
        Proto += " const";
697
7.47k
      if (FT->isVolatile())
698
2
        Proto += " volatile";
699
7.47k
      if (FT->isRestrict())
700
0
        Proto += " restrict";
701
702
7.47k
      switch (FT->getRefQualifier()) {
703
7.47k
      case RQ_None:
704
7.47k
        break;
705
1
      case RQ_LValue:
706
1
        Proto += " &";
707
1
        break;
708
1
      case RQ_RValue:
709
1
        Proto += " &&";
710
1
        break;
711
7.47k
      }
712
7.47k
    }
713
714
7.70k
    if (FT && 
FT->hasDynamicExceptionSpec()7.47k
) {
715
3
      Proto += " throw(";
716
3
      if (FT->getExceptionSpecType() == EST_MSAny)
717
0
        Proto += "...";
718
3
      else
719
6
        
for (unsigned I = 0, N = FT->getNumExceptions(); 3
I != N;
++I3
) {
720
3
          if (I)
721
1
            Proto += ", ";
722
723
3
          Proto += FT->getExceptionType(I).getAsString(SubPolicy);
724
3
        }
725
3
      Proto += ")";
726
7.70k
    } else if (FT && 
isNoexceptExceptionSpec(FT->getExceptionSpecType())7.47k
) {
727
51
      Proto += " noexcept";
728
51
      if (isComputedNoexcept(FT->getExceptionSpecType())) {
729
3
        Proto += "(";
730
3
        llvm::raw_string_ostream EOut(Proto);
731
3
        FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
732
3
                                           Indentation, "\n", &Context);
733
3
        EOut.flush();
734
3
        Proto += EOut.str();
735
3
        Proto += ")";
736
3
      }
737
51
    }
738
739
7.70k
    if (CDecl) {
740
1.84k
      if (!Policy.TerseOutput)
741
1.70k
        PrintConstructorInitializers(CDecl, Proto);
742
5.86k
    } else if (!ConversionDecl && 
!isa<CXXDestructorDecl>(D)5.69k
) {
743
5.58k
      if (FT && 
FT->hasTrailingReturn()5.35k
) {
744
14
        if (!GuideDecl)
745
7
          Out << "auto ";
746
14
        Out << Proto << " -> ";
747
14
        Proto.clear();
748
14
      }
749
5.58k
      AFT->getReturnType().print(Out, Policy, Proto);
750
5.58k
      Proto.clear();
751
5.58k
    }
752
7.70k
    Out << Proto;
753
754
7.70k
    if (Expr *TrailingRequiresClause = D->getTrailingRequiresClause()) {
755
2
      Out << " requires ";
756
2
      TrailingRequiresClause->printPretty(Out, nullptr, SubPolicy, Indentation,
757
2
                                          "\n", &Context);
758
2
    }
759
7.70k
  } else {
760
0
    Ty.print(Out, Policy, Proto);
761
0
  }
762
763
7.70k
  prettyPrintAttributes(D);
764
765
7.70k
  if (D->isPure())
766
2
    Out << " = 0";
767
7.70k
  else if (D->isDeletedAsWritten())
768
1
    Out << " = delete";
769
7.70k
  else if (D->isExplicitlyDefaulted())
770
5
    Out << " = default";
771
7.70k
  else if (D->doesThisDeclarationHaveABody()) {
772
5.76k
    if (!Policy.TerseOutput) {
773
4.96k
      if (!D->hasPrototype() && 
D->getNumParams()174
) {
774
        // This is a K&R function definition, so we need to print the
775
        // parameters.
776
5
        Out << '\n';
777
5
        DeclPrinter ParamPrinter(Out, SubPolicy, Context, Indentation);
778
5
        Indentation += Policy.Indentation;
779
11
        for (unsigned i = 0, e = D->getNumParams(); i != e; 
++i6
) {
780
6
          Indent();
781
6
          ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
782
6
          Out << ";\n";
783
6
        }
784
5
        Indentation -= Policy.Indentation;
785
5
      }
786
787
4.96k
      if (D->getBody())
788
4.96k
        D->getBody()->printPrettyControlled(Out, nullptr, SubPolicy, Indentation, "\n",
789
4.96k
                                  &Context);
790
4.96k
    } else {
791
808
      if (!Policy.TerseOutput && 
isa<CXXConstructorDecl>(*D)0
)
792
0
        Out << " {}";
793
808
    }
794
5.76k
  }
795
7.70k
}
796
797
47
void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
798
47
  if (TypeSourceInfo *TSI = D->getFriendType()) {
799
9
    unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
800
9
    for (unsigned i = 0; i < NumTPLists; 
++i0
)
801
0
      printTemplateParameters(D->getFriendTypeTemplateParameterList(i));
802
9
    Out << "friend ";
803
9
    Out << " " << TSI->getType().getAsString(Policy);
804
9
  }
805
38
  else if (FunctionDecl *FD =
806
38
      dyn_cast<FunctionDecl>(D->getFriendDecl())) {
807
23
    Out << "friend ";
808
23
    VisitFunctionDecl(FD);
809
23
  }
810
15
  else if (FunctionTemplateDecl *FTD =
811
15
           dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
812
9
    Out << "friend ";
813
9
    VisitFunctionTemplateDecl(FTD);
814
9
  }
815
6
  else if (ClassTemplateDecl *CTD =
816
6
           dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
817
6
    Out << "friend ";
818
6
    VisitRedeclarableTemplateDecl(CTD);
819
6
  }
820
47
}
821
822
2.30k
void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
823
  // FIXME: add printing of pragma attributes if required.
824
2.30k
  if (!Policy.SuppressSpecifiers && 
D->isMutable()2.29k
)
825
36
    Out << "mutable ";
826
2.30k
  if (!Policy.SuppressSpecifiers && 
D->isModulePrivate()2.29k
)
827
0
    Out << "__module_private__ ";
828
829
2.30k
  Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
830
2.30k
         stream(Policy, D->getName(), Indentation);
831
832
2.30k
  if (D->isBitField()) {
833
84
    Out << " : ";
834
84
    D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation, "\n",
835
84
                                  &Context);
836
84
  }
837
838
2.30k
  Expr *Init = D->getInClassInitializer();
839
2.30k
  if (!Policy.SuppressInitializers && Init) {
840
212
    if (D->getInClassInitStyle() == ICIS_ListInit)
841
0
      Out << " ";
842
212
    else
843
212
      Out << " = ";
844
212
    Init->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context);
845
212
  }
846
2.30k
  prettyPrintAttributes(D);
847
2.30k
}
848
849
0
void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
850
0
  Out << *D << ":";
851
0
}
852
853
26.1k
void DeclPrinter::VisitVarDecl(VarDecl *D) {
854
26.1k
  prettyPrintPragmas(D);
855
856
26.1k
  QualType T = D->getTypeSourceInfo()
857
26.1k
    ? 
D->getTypeSourceInfo()->getType()26.0k
858
26.1k
    : 
D->getASTContext().getUnqualifiedObjCPointerType(D->getType())109
;
859
860
26.1k
  if (!Policy.SuppressSpecifiers) {
861
21.1k
    StorageClass SC = D->getStorageClass();
862
21.1k
    if (SC != SC_None)
863
2.09k
      Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
864
865
21.1k
    switch (D->getTSCSpec()) {
866
21.1k
    case TSCS_unspecified:
867
21.1k
      break;
868
1
    case TSCS___thread:
869
1
      Out << "__thread ";
870
1
      break;
871
1
    case TSCS__Thread_local:
872
1
      Out << "_Thread_local ";
873
1
      break;
874
6
    case TSCS_thread_local:
875
6
      Out << "thread_local ";
876
6
      break;
877
21.1k
    }
878
879
21.1k
    if (D->isModulePrivate())
880
0
      Out << "__module_private__ ";
881
882
21.1k
    if (D->isConstexpr()) {
883
17
      Out << "constexpr ";
884
17
      T.removeLocalConst();
885
17
    }
886
21.1k
  }
887
888
26.1k
  printDeclType(T, D->getName());
889
26.1k
  Expr *Init = D->getInit();
890
26.1k
  if (!Policy.SuppressInitializers && 
Init26.1k
) {
891
11.0k
    bool ImplicitInit = false;
892
11.0k
    if (CXXConstructExpr *Construct =
893
11.0k
            dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
894
1.14k
      if (D->getInitStyle() == VarDecl::CallInit &&
895
1.14k
          
!Construct->isListInitialization()919
) {
896
919
        ImplicitInit = Construct->getNumArgs() == 0 ||
897
919
          
Construct->getArg(0)->isDefaultArgument()52
;
898
919
      }
899
1.14k
    }
900
11.0k
    if (!ImplicitInit) {
901
10.1k
      if ((D->getInitStyle() == VarDecl::CallInit) && 
!isa<ParenListExpr>(Init)53
)
902
52
        Out << "(";
903
10.1k
      else if (D->getInitStyle() == VarDecl::CInit) {
904
10.0k
        Out << " = ";
905
10.0k
      }
906
10.1k
      PrintingPolicy SubPolicy(Policy);
907
10.1k
      SubPolicy.SuppressSpecifiers = false;
908
10.1k
      SubPolicy.IncludeTagDefinition = false;
909
10.1k
      Init->printPretty(Out, nullptr, SubPolicy, Indentation, "\n", &Context);
910
10.1k
      if ((D->getInitStyle() == VarDecl::CallInit) && 
!isa<ParenListExpr>(Init)53
)
911
52
        Out << ")";
912
10.1k
    }
913
11.0k
  }
914
26.1k
  prettyPrintAttributes(D);
915
26.1k
}
916
917
5.57k
void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
918
5.57k
  VisitVarDecl(D);
919
5.57k
}
920
921
2
void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
922
2
  Out << "__asm (";
923
2
  D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation, "\n",
924
2
                                 &Context);
925
2
  Out << ")";
926
2
}
927
928
17
void DeclPrinter::VisitImportDecl(ImportDecl *D) {
929
17
  Out << "@import " << D->getImportedModule()->getFullModuleName()
930
17
      << ";\n";
931
17
}
932
933
3
void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
934
3
  Out << "static_assert(";
935
3
  D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation, "\n",
936
3
                                  &Context);
937
3
  if (StringLiteral *SL = D->getMessage()) {
938
2
    Out << ", ";
939
2
    SL->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context);
940
2
  }
941
3
  Out << ")";
942
3
}
943
944
//----------------------------------------------------------------------------
945
// C++ declarations
946
//----------------------------------------------------------------------------
947
139
void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
948
139
  if (D->isInline())
949
1
    Out << "inline ";
950
951
139
  Out << "namespace ";
952
139
  if (D->getDeclName())
953
136
    Out << D->getDeclName() << ' ';
954
139
  Out << "{\n";
955
956
139
  VisitDeclContext(D);
957
139
  Indent() << "}";
958
139
}
959
960
5
void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
961
5
  Out << "using namespace ";
962
5
  if (D->getQualifier())
963
3
    D->getQualifier()->print(Out, Policy);
964
5
  Out << *D->getNominatedNamespaceAsWritten();
965
5
}
966
967
4
void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
968
4
  Out << "namespace " << *D << " = ";
969
4
  if (D->getQualifier())
970
3
    D->getQualifier()->print(Out, Policy);
971
4
  Out << *D->getAliasedNamespace();
972
4
}
973
974
18
void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
975
18
  prettyPrintAttributes(D);
976
18
}
977
978
2.03k
void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
979
  // FIXME: add printing of pragma attributes if required.
980
2.03k
  if (!Policy.SuppressSpecifiers && D->isModulePrivate())
981
0
    Out << "__module_private__ ";
982
2.03k
  Out << D->getKindName();
983
984
2.03k
  prettyPrintAttributes(D);
985
986
2.03k
  if (D->getIdentifier()) {
987
2.00k
    Out << ' ' << *D;
988
989
2.00k
    if (auto S = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
990
420
      ArrayRef<TemplateArgument> Args = S->getTemplateArgs().asArray();
991
420
      if (!Policy.PrintCanonicalTypes)
992
420
        if (const auto* TSI = S->getTypeAsWritten())
993
54
          if (const auto *TST =
994
54
                  dyn_cast<TemplateSpecializationType>(TSI->getType()))
995
54
            Args = TST->template_arguments();
996
420
      printTemplateArguments(
997
420
          Args, S->getSpecializedTemplate()->getTemplateParameters());
998
420
    }
999
2.00k
  }
1000
1001
2.03k
  if (D->isCompleteDefinition()) {
1002
    // Print the base classes
1003
1.92k
    if (D->getNumBases()) {
1004
513
      Out << " : ";
1005
513
      for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
1006
1.03k
             BaseEnd = D->bases_end(); Base != BaseEnd; 
++Base520
) {
1007
520
        if (Base != D->bases_begin())
1008
7
          Out << ", ";
1009
1010
520
        if (Base->isVirtual())
1011
13
          Out << "virtual ";
1012
1013
520
        AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
1014
520
        if (AS != AS_none) {
1015
485
          Print(AS);
1016
485
          Out << " ";
1017
485
        }
1018
520
        Out << Base->getType().getAsString(Policy);
1019
1020
520
        if (Base->isPackExpansion())
1021
1
          Out << "...";
1022
520
      }
1023
513
    }
1024
1025
    // Print the class definition
1026
    // FIXME: Doesn't print access specifiers, e.g., "public:"
1027
1.92k
    if (Policy.TerseOutput) {
1028
101
      Out << " {}";
1029
1.82k
    } else {
1030
1.82k
      Out << " {\n";
1031
1.82k
      VisitDeclContext(D);
1032
1.82k
      Indent() << "}";
1033
1.82k
    }
1034
1.92k
  }
1035
2.03k
}
1036
1037
28
void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
1038
28
  const char *l;
1039
28
  if (D->getLanguage() == LinkageSpecDecl::lang_c)
1040
18
    l = "C";
1041
10
  else {
1042
10
    assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
1043
10
           "unknown language in linkage specification");
1044
0
    l = "C++";
1045
10
  }
1046
1047
0
  Out << "extern \"" << l << "\" ";
1048
28
  if (D->hasBraces()) {
1049
22
    Out << "{\n";
1050
22
    VisitDeclContext(D);
1051
22
    Indent() << "}";
1052
22
  } else
1053
6
    Visit(*D->decls_begin());
1054
28
}
1055
1056
void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params,
1057
1.25k
                                          bool OmitTemplateKW) {
1058
1.25k
  assert(Params);
1059
1060
1.25k
  if (!OmitTemplateKW)
1061
1.25k
    Out << "template ";
1062
1.25k
  Out << '<';
1063
1064
1.25k
  bool NeedComma = false;
1065
1.65k
  for (const Decl *Param : *Params) {
1066
1.65k
    if (Param->isImplicit())
1067
8
      continue;
1068
1069
1.65k
    if (NeedComma)
1070
400
      Out << ", ";
1071
1.25k
    else
1072
1.25k
      NeedComma = true;
1073
1074
1.65k
    if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
1075
1.24k
      VisitTemplateTypeParmDecl(TTP);
1076
1.24k
    } else 
if (auto 402
NTTP402
= dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1077
373
      VisitNonTypeTemplateParmDecl(NTTP);
1078
373
    } else 
if (auto 29
TTPD29
= dyn_cast<TemplateTemplateParmDecl>(Param)) {
1079
29
      VisitTemplateDecl(TTPD);
1080
      // FIXME: print the default argument, if present.
1081
29
    }
1082
1.65k
  }
1083
1084
1.25k
  Out << '>';
1085
1.25k
  if (!OmitTemplateKW)
1086
1.25k
    Out << ' ';
1087
1.25k
}
1088
1089
void DeclPrinter::printTemplateArguments(ArrayRef<TemplateArgument> Args,
1090
1.07k
                                         const TemplateParameterList *Params) {
1091
1.07k
  Out << "<";
1092
2.59k
  for (size_t I = 0, E = Args.size(); I < E; 
++I1.51k
) {
1093
1.51k
    if (I)
1094
434
      Out << ", ";
1095
1.51k
    if (!Params)
1096
1.05k
      Args[I].print(Policy, Out, /*IncludeType*/ true);
1097
463
    else
1098
463
      Args[I].print(Policy, Out,
1099
463
                    TemplateParameterList::shouldIncludeTypeForArgument(
1100
463
                        Policy, Params, I));
1101
1.51k
  }
1102
1.07k
  Out << ">";
1103
1.07k
}
1104
1105
void DeclPrinter::printTemplateArguments(ArrayRef<TemplateArgumentLoc> Args,
1106
19
                                         const TemplateParameterList *Params) {
1107
19
  Out << "<";
1108
38
  for (size_t I = 0, E = Args.size(); I < E; 
++I19
) {
1109
19
    if (I)
1110
0
      Out << ", ";
1111
19
    if (!Params)
1112
19
      Args[I].getArgument().print(Policy, Out, /*IncludeType*/ true);
1113
0
    else
1114
0
      Args[I].getArgument().print(
1115
0
          Policy, Out,
1116
0
          TemplateParameterList::shouldIncludeTypeForArgument(Policy, Params,
1117
0
                                                              I));
1118
19
  }
1119
19
  Out << ">";
1120
19
}
1121
1122
1.22k
void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
1123
1.22k
  printTemplateParameters(D->getTemplateParameters());
1124
1125
1.22k
  if (const TemplateTemplateParmDecl *TTP =
1126
1.22k
        dyn_cast<TemplateTemplateParmDecl>(D)) {
1127
30
    Out << "class";
1128
1129
30
    if (TTP->isParameterPack())
1130
2
      Out << " ...";
1131
28
    else if (TTP->getDeclName())
1132
27
      Out << ' ';
1133
1134
30
    if (TTP->getDeclName())
1135
28
      Out << TTP->getDeclName();
1136
1.19k
  } else if (auto *TD = D->getTemplatedDecl())
1137
1.19k
    Visit(TD);
1138
8
  else if (const auto *Concept = dyn_cast<ConceptDecl>(D)) {
1139
8
    Out << "concept " << Concept->getName() << " = " ;
1140
8
    Concept->getConstraintExpr()->printPretty(Out, nullptr, Policy, Indentation,
1141
8
                                              "\n", &Context);
1142
8
  }
1143
1.22k
}
1144
1145
666
void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
1146
666
  prettyPrintPragmas(D->getTemplatedDecl());
1147
  // Print any leading template parameter lists.
1148
666
  if (const FunctionDecl *FD = D->getTemplatedDecl()) {
1149
666
    for (unsigned I = 0, NumTemplateParams = FD->getNumTemplateParameterLists();
1150
667
         I < NumTemplateParams; 
++I1
)
1151
1
      printTemplateParameters(FD->getTemplateParameterList(I));
1152
666
  }
1153
666
  VisitRedeclarableTemplateDecl(D);
1154
  // Declare target attribute is special one, natural spelling for the pragma
1155
  // assumes "ending" construct so print it here.
1156
666
  if (D->getTemplatedDecl()->hasAttr<OMPDeclareTargetDeclAttr>())
1157
8
    Out << "#pragma omp end declare target\n";
1158
1159
  // Never print "instantiations" for deduction guides (they don't really
1160
  // have them).
1161
666
  if (PrintInstantiation &&
1162
666
      
!isa<CXXDeductionGuideDecl>(D->getTemplatedDecl())535
) {
1163
531
    FunctionDecl *PrevDecl = D->getTemplatedDecl();
1164
531
    const FunctionDecl *Def;
1165
531
    if (PrevDecl->isDefined(Def) && 
Def != PrevDecl461
)
1166
7
      return;
1167
524
    for (auto *I : D->specializations())
1168
673
      if (I->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) {
1169
632
        if (!PrevDecl->isThisDeclarationADefinition())
1170
59
          Out << ";\n";
1171
632
        Indent();
1172
632
        prettyPrintPragmas(I);
1173
632
        Visit(I);
1174
632
      }
1175
524
  }
1176
666
}
1177
1178
491
void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
1179
491
  VisitRedeclarableTemplateDecl(D);
1180
1181
491
  if (PrintInstantiation) {
1182
382
    for (auto *I : D->specializations())
1183
395
      if (I->getSpecializationKind() == TSK_ImplicitInstantiation) {
1184
366
        if (D->isThisDeclarationADefinition())
1185
362
          Out << ";";
1186
366
        Out << "\n";
1187
366
        Indent();
1188
366
        Visit(I);
1189
366
      }
1190
382
  }
1191
491
}
1192
1193
void DeclPrinter::VisitClassTemplateSpecializationDecl(
1194
400
                                           ClassTemplateSpecializationDecl *D) {
1195
400
  Out << "template<> ";
1196
400
  VisitCXXRecordDecl(D);
1197
400
}
1198
1199
void DeclPrinter::VisitClassTemplatePartialSpecializationDecl(
1200
20
                                    ClassTemplatePartialSpecializationDecl *D) {
1201
20
  printTemplateParameters(D->getTemplateParameters());
1202
20
  VisitCXXRecordDecl(D);
1203
20
}
1204
1205
//----------------------------------------------------------------------------
1206
// Objective-C declarations
1207
//----------------------------------------------------------------------------
1208
1209
void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
1210
                                      Decl::ObjCDeclQualifier Quals,
1211
2.31k
                                      QualType T) {
1212
2.31k
  Out << '(';
1213
2.31k
  if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
1214
58
    Out << "in ";
1215
2.31k
  if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
1216
0
    Out << "inout ";
1217
2.31k
  if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
1218
0
    Out << "out ";
1219
2.31k
  if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
1220
0
    Out << "bycopy ";
1221
2.31k
  if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
1222
0
    Out << "byref ";
1223
2.31k
  if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
1224
58
    Out << "oneway ";
1225
2.31k
  if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
1226
279
    if (auto nullability = AttributedType::stripOuterNullability(T))
1227
279
      Out << getNullabilitySpelling(*nullability, true) << ' ';
1228
279
  }
1229
1230
2.31k
  Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
1231
2.31k
  Out << ')';
1232
2.31k
}
1233
1234
1
void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1235
1
  Out << "<";
1236
1
  unsigned First = true;
1237
2
  for (auto *Param : *Params) {
1238
2
    if (First) {
1239
1
      First = false;
1240
1
    } else {
1241
1
      Out << ", ";
1242
1
    }
1243
1244
2
    switch (Param->getVariance()) {
1245
1
    case ObjCTypeParamVariance::Invariant:
1246
1
      break;
1247
1248
1
    case ObjCTypeParamVariance::Covariant:
1249
1
      Out << "__covariant ";
1250
1
      break;
1251
1252
0
    case ObjCTypeParamVariance::Contravariant:
1253
0
      Out << "__contravariant ";
1254
0
      break;
1255
2
    }
1256
1257
2
    Out << Param->getDeclName();
1258
1259
2
    if (Param->hasExplicitBound()) {
1260
2
      Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1261
2
    }
1262
2
  }
1263
1
  Out << ">";
1264
1
}
1265
1266
1.55k
void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1267
1.55k
  if (OMD->isInstanceMethod())
1268
1.26k
    Out << "- ";
1269
292
  else
1270
292
    Out << "+ ";
1271
1.55k
  if (!OMD->getReturnType().isNull()) {
1272
1.55k
    PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
1273
1.55k
                        OMD->getReturnType());
1274
1.55k
  }
1275
1276
1.55k
  std::string name = OMD->getSelector().getAsString();
1277
1.55k
  std::string::size_type pos, lastPos = 0;
1278
1.55k
  for (const auto *PI : OMD->parameters()) {
1279
    // FIXME: selector is missing here!
1280
764
    pos = name.find_first_of(':', lastPos);
1281
764
    if (lastPos != 0)
1282
88
      Out << " ";
1283
764
    Out << name.substr(lastPos, pos - lastPos) << ':';
1284
764
    PrintObjCMethodType(OMD->getASTContext(),
1285
764
                        PI->getObjCDeclQualifier(),
1286
764
                        PI->getType());
1287
764
    Out << *PI;
1288
764
    lastPos = pos + 1;
1289
764
  }
1290
1291
1.55k
  if (OMD->param_begin() == OMD->param_end())
1292
877
    Out << name;
1293
1294
1.55k
  if (OMD->isVariadic())
1295
1
      Out << ", ...";
1296
1297
1.55k
  prettyPrintAttributes(OMD);
1298
1299
1.55k
  if (OMD->getBody() && 
!Policy.TerseOutput20
) {
1300
9
    Out << ' ';
1301
9
    OMD->getBody()->printPretty(Out, nullptr, Policy, Indentation, "\n",
1302
9
                                &Context);
1303
9
  }
1304
1.54k
  else if (Policy.PolishForDeclaration)
1305
73
    Out << ';';
1306
1.55k
}
1307
1308
8
void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1309
8
  std::string I = OID->getNameAsString();
1310
8
  ObjCInterfaceDecl *SID = OID->getSuperClass();
1311
1312
8
  bool eolnOut = false;
1313
8
  if (SID)
1314
1
    Out << "@implementation " << I << " : " << *SID;
1315
7
  else
1316
7
    Out << "@implementation " << I;
1317
1318
8
  if (OID->ivar_size() > 0) {
1319
2
    Out << "{\n";
1320
2
    eolnOut = true;
1321
2
    Indentation += Policy.Indentation;
1322
5
    for (const auto *I : OID->ivars()) {
1323
5
      Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
1324
5
                    getAsString(Policy) << ' ' << *I << ";\n";
1325
5
    }
1326
2
    Indentation -= Policy.Indentation;
1327
2
    Out << "}\n";
1328
2
  }
1329
6
  else if (SID || 
(OID->decls_begin() != OID->decls_end())5
) {
1330
5
    Out << "\n";
1331
5
    eolnOut = true;
1332
5
  }
1333
8
  VisitDeclContext(OID, false);
1334
8
  if (!eolnOut)
1335
1
    Out << "\n";
1336
8
  Out << "@end";
1337
8
}
1338
1339
75
void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1340
75
  std::string I = OID->getNameAsString();
1341
75
  ObjCInterfaceDecl *SID = OID->getSuperClass();
1342
1343
75
  if (!OID->isThisDeclarationADefinition()) {
1344
3
    Out << "@class " << I;
1345
1346
3
    if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1347
0
      PrintObjCTypeParams(TypeParams);
1348
0
    }
1349
1350
3
    Out << ";";
1351
3
    return;
1352
3
  }
1353
72
  bool eolnOut = false;
1354
72
  Out << "@interface " << I;
1355
1356
72
  if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1357
1
    PrintObjCTypeParams(TypeParams);
1358
1
  }
1359
1360
72
  if (SID)
1361
28
    Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
1362
1363
  // Protocols?
1364
72
  const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1365
72
  if (!Protocols.empty()) {
1366
6
    for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1367
12
         E = Protocols.end(); I != E; 
++I6
)
1368
6
      Out << (I == Protocols.begin() ? '<' : 
','0
) << **I;
1369
6
    Out << "> ";
1370
6
  }
1371
1372
72
  if (OID->ivar_size() > 0) {
1373
21
    Out << "{\n";
1374
21
    eolnOut = true;
1375
21
    Indentation += Policy.Indentation;
1376
45
    for (const auto *I : OID->ivars()) {
1377
45
      Indent() << I->getASTContext()
1378
45
                      .getUnqualifiedObjCPointerType(I->getType())
1379
45
                      .getAsString(Policy) << ' ' << *I << ";\n";
1380
45
    }
1381
21
    Indentation -= Policy.Indentation;
1382
21
    Out << "}\n";
1383
21
  }
1384
51
  else if (SID || 
(OID->decls_begin() != OID->decls_end())35
) {
1385
33
    Out << "\n";
1386
33
    eolnOut = true;
1387
33
  }
1388
1389
72
  VisitDeclContext(OID, false);
1390
72
  if (!eolnOut)
1391
18
    Out << "\n";
1392
72
  Out << "@end";
1393
  // FIXME: implement the rest...
1394
72
}
1395
1396
13
void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
1397
13
  if (!PID->isThisDeclarationADefinition()) {
1398
3
    Out << "@protocol " << *PID << ";\n";
1399
3
    return;
1400
3
  }
1401
  // Protocols?
1402
10
  const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1403
10
  if (!Protocols.empty()) {
1404
1
    Out << "@protocol " << *PID;
1405
1
    for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1406
2
         E = Protocols.end(); I != E; 
++I1
)
1407
1
      Out << (I == Protocols.begin() ? '<' : 
','0
) << **I;
1408
1
    Out << ">\n";
1409
1
  } else
1410
9
    Out << "@protocol " << *PID << '\n';
1411
10
  VisitDeclContext(PID, false);
1412
10
  Out << "@end";
1413
10
}
1414
1415
3
void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
1416
3
  Out << "@implementation ";
1417
3
  if (const auto *CID = PID->getClassInterface())
1418
2
    Out << *CID;
1419
1
  else
1420
1
    Out << "<<error-type>>";
1421
3
  Out << '(' << *PID << ")\n";
1422
1423
3
  VisitDeclContext(PID, false);
1424
3
  Out << "@end";
1425
  // FIXME: implement the rest...
1426
3
}
1427
1428
11
void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
1429
11
  Out << "@interface ";
1430
11
  if (const auto *CID = PID->getClassInterface())
1431
10
    Out << *CID;
1432
1
  else
1433
1
    Out << "<<error-type>>";
1434
11
  if (auto TypeParams = PID->getTypeParamList()) {
1435
0
    PrintObjCTypeParams(TypeParams);
1436
0
  }
1437
11
  Out << "(" << *PID << ")\n";
1438
11
  if (PID->ivar_size() > 0) {
1439
1
    Out << "{\n";
1440
1
    Indentation += Policy.Indentation;
1441
1
    for (const auto *I : PID->ivars())
1442
1
      Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
1443
1
                    getAsString(Policy) << ' ' << *I << ";\n";
1444
1
    Indentation -= Policy.Indentation;
1445
1
    Out << "}\n";
1446
1
  }
1447
1448
11
  VisitDeclContext(PID, false);
1449
11
  Out << "@end";
1450
1451
  // FIXME: implement the rest...
1452
11
}
1453
1454
1
void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
1455
1
  Out << "@compatibility_alias " << *AID
1456
1
      << ' ' << *AID->getClassInterface() << ";\n";
1457
1
}
1458
1459
/// PrintObjCPropertyDecl - print a property declaration.
1460
///
1461
/// Print attributes in the following order:
1462
/// - class
1463
/// - nonatomic | atomic
1464
/// - assign | retain | strong | copy | weak | unsafe_unretained
1465
/// - readwrite | readonly
1466
/// - getter & setter
1467
/// - nullability
1468
48
void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1469
48
  if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1470
0
    Out << "@required\n";
1471
48
  else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1472
1
    Out << "@optional\n";
1473
1474
48
  QualType T = PDecl->getType();
1475
1476
48
  Out << "@property";
1477
48
  if (PDecl->getPropertyAttributes() != ObjCPropertyAttribute::kind_noattr) {
1478
48
    bool first = true;
1479
48
    Out << "(";
1480
48
    if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_class) {
1481
1
      Out << (first ? "" : 
", "0
) << "class";
1482
1
      first = false;
1483
1
    }
1484
1485
48
    if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_direct) {
1486
0
      Out << (first ? "" : ", ") << "direct";
1487
0
      first = false;
1488
0
    }
1489
1490
48
    if (PDecl->getPropertyAttributes() &
1491
48
        ObjCPropertyAttribute::kind_nonatomic) {
1492
19
      Out << (first ? "" : 
", "0
) << "nonatomic";
1493
19
      first = false;
1494
19
    }
1495
48
    if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_atomic) {
1496
17
      Out << (first ? 
""16
:
", "1
) << "atomic";
1497
17
      first = false;
1498
17
    }
1499
1500
48
    if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_assign) {
1501
10
      Out << (first ? 
""3
:
", "7
) << "assign";
1502
10
      first = false;
1503
10
    }
1504
48
    if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_retain) {
1505
5
      Out << (first ? 
""3
:
", "2
) << "retain";
1506
5
      first = false;
1507
5
    }
1508
1509
48
    if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_strong) {
1510
1
      Out << (first ? 
""0
: ", ") << "strong";
1511
1
      first = false;
1512
1
    }
1513
48
    if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_copy) {
1514
21
      Out << (first ? 
""0
: ", ") << "copy";
1515
21
      first = false;
1516
21
    }
1517
48
    if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_weak) {
1518
1
      Out << (first ? 
""0
: ", ") << "weak";
1519
1
      first = false;
1520
1
    }
1521
48
    if (PDecl->getPropertyAttributes() &
1522
48
        ObjCPropertyAttribute::kind_unsafe_unretained) {
1523
7
      Out << (first ? 
""0
: ", ") << "unsafe_unretained";
1524
7
      first = false;
1525
7
    }
1526
1527
48
    if (PDecl->getPropertyAttributes() &
1528
48
        ObjCPropertyAttribute::kind_readwrite) {
1529
38
      Out << (first ? 
""0
: ", ") << "readwrite";
1530
38
      first = false;
1531
38
    }
1532
48
    if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_readonly) {
1533
10
      Out << (first ? 
""6
:
", "4
) << "readonly";
1534
10
      first = false;
1535
10
    }
1536
1537
48
    if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_getter) {
1538
16
      Out << (first ? 
""0
: ", ") << "getter = ";
1539
16
      PDecl->getGetterName().print(Out);
1540
16
      first = false;
1541
16
    }
1542
48
    if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_setter) {
1543
15
      Out << (first ? 
""0
: ", ") << "setter = ";
1544
15
      PDecl->getSetterName().print(Out);
1545
15
      first = false;
1546
15
    }
1547
1548
48
    if (PDecl->getPropertyAttributes() &
1549
48
        ObjCPropertyAttribute::kind_nullability) {
1550
4
      if (auto nullability = AttributedType::stripOuterNullability(T)) {
1551
4
        if (*nullability == NullabilityKind::Unspecified &&
1552
4
            (PDecl->getPropertyAttributes() &
1553
1
             ObjCPropertyAttribute::kind_null_resettable)) {
1554
1
          Out << (first ? 
""0
: ", ") << "null_resettable";
1555
3
        } else {
1556
3
          Out << (first ? 
""0
: ", ")
1557
3
              << getNullabilitySpelling(*nullability, true);
1558
3
        }
1559
4
        first = false;
1560
4
      }
1561
4
    }
1562
1563
48
    (void) first; // Silence dead store warning due to idiomatic code.
1564
48
    Out << ")";
1565
48
  }
1566
48
  std::string TypeStr = PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
1567
48
      getAsString(Policy);
1568
48
  Out << ' ' << TypeStr;
1569
48
  if (!StringRef(TypeStr).endswith("*"))
1570
28
    Out << ' ';
1571
48
  Out << *PDecl;
1572
48
  if (Policy.PolishForDeclaration)
1573
21
    Out << ';';
1574
48
}
1575
1576
6
void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1577
6
  if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1578
5
    Out << "@synthesize ";
1579
1
  else
1580
1
    Out << "@dynamic ";
1581
6
  Out << *PID->getPropertyDecl();
1582
6
  if (PID->getPropertyIvarDecl())
1583
5
    Out << '=' << *PID->getPropertyIvarDecl();
1584
6
}
1585
1586
19
void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
1587
19
  if (!D->isAccessDeclaration())
1588
19
    Out << "using ";
1589
19
  if (D->hasTypename())
1590
0
    Out << "typename ";
1591
19
  D->getQualifier()->print(Out, Policy);
1592
1593
  // Use the correct record name when the using declaration is used for
1594
  // inheriting constructors.
1595
19
  for (const auto *Shadow : D->shadows()) {
1596
19
    if (const auto *ConstructorShadow =
1597
19
            dyn_cast<ConstructorUsingShadowDecl>(Shadow)) {
1598
4
      assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext());
1599
0
      Out << *ConstructorShadow->getNominatedBaseClass();
1600
4
      return;
1601
4
    }
1602
19
  }
1603
15
  Out << *D;
1604
15
}
1605
1606
8
void DeclPrinter::VisitUsingEnumDecl(UsingEnumDecl *D) {
1607
8
  Out << "using enum " << D->getEnumDecl();
1608
8
}
1609
1610
void
1611
0
DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1612
0
  Out << "using typename ";
1613
0
  D->getQualifier()->print(Out, Policy);
1614
0
  Out << D->getDeclName();
1615
0
}
1616
1617
5
void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1618
5
  if (!D->isAccessDeclaration())
1619
5
    Out << "using ";
1620
5
  D->getQualifier()->print(Out, Policy);
1621
5
  Out << D->getDeclName();
1622
5
}
1623
1624
0
void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1625
  // ignore
1626
0
}
1627
1628
448
void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1629
448
  Out << "#pragma omp threadprivate";
1630
448
  if (!D->varlist_empty()) {
1631
448
    for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1632
448
                                                E = D->varlist_end();
1633
904
                                                I != E; 
++I456
) {
1634
456
      Out << (I == D->varlist_begin() ? 
'('448
:
','8
);
1635
456
      NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl();
1636
456
      ND->printQualifiedName(Out);
1637
456
    }
1638
448
    Out << ")";
1639
448
  }
1640
448
}
1641
1642
182
void DeclPrinter::VisitOMPAllocateDecl(OMPAllocateDecl *D) {
1643
182
  Out << "#pragma omp allocate";
1644
182
  if (!D->varlist_empty()) {
1645
182
    for (OMPAllocateDecl::varlist_iterator I = D->varlist_begin(),
1646
182
                                           E = D->varlist_end();
1647
380
         I != E; 
++I198
) {
1648
198
      Out << (I == D->varlist_begin() ? 
'('182
:
','16
);
1649
198
      NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl();
1650
198
      ND->printQualifiedName(Out);
1651
198
    }
1652
182
    Out << ")";
1653
182
  }
1654
182
  if (!D->clauselist_empty()) {
1655
94
    OMPClausePrinter Printer(Out, Policy);
1656
98
    for (OMPClause *C : D->clauselists()) {
1657
98
      Out << " ";
1658
98
      Printer.Visit(C);
1659
98
    }
1660
94
  }
1661
182
}
1662
1663
28
void DeclPrinter::VisitOMPRequiresDecl(OMPRequiresDecl *D) {
1664
28
  Out << "#pragma omp requires ";
1665
28
  if (!D->clauselist_empty()) {
1666
28
    OMPClausePrinter Printer(Out, Policy);
1667
56
    for (auto I = D->clauselist_begin(), E = D->clauselist_end(); I != E; 
++I28
)
1668
28
      Printer.Visit(*I);
1669
28
  }
1670
28
}
1671
1672
92
void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1673
92
  if (!D->isInvalidDecl()) {
1674
92
    Out << "#pragma omp declare reduction (";
1675
92
    if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) {
1676
32
      const char *OpName =
1677
32
          getOperatorSpelling(D->getDeclName().getCXXOverloadedOperator());
1678
32
      assert(OpName && "not an overloaded operator");
1679
0
      Out << OpName;
1680
60
    } else {
1681
60
      assert(D->getDeclName().isIdentifier());
1682
0
      D->printName(Out);
1683
60
    }
1684
0
    Out << " : ";
1685
92
    D->getType().print(Out, Policy);
1686
92
    Out << " : ";
1687
92
    D->getCombiner()->printPretty(Out, nullptr, Policy, 0, "\n", &Context);
1688
92
    Out << ")";
1689
92
    if (auto *Init = D->getInitializer()) {
1690
56
      Out << " initializer(";
1691
56
      switch (D->getInitializerKind()) {
1692
0
      case OMPDeclareReductionDecl::DirectInit:
1693
0
        Out << "omp_priv(";
1694
0
        break;
1695
40
      case OMPDeclareReductionDecl::CopyInit:
1696
40
        Out << "omp_priv = ";
1697
40
        break;
1698
16
      case OMPDeclareReductionDecl::CallInit:
1699
16
        break;
1700
56
      }
1701
56
      Init->printPretty(Out, nullptr, Policy, 0, "\n", &Context);
1702
56
      if (D->getInitializerKind() == OMPDeclareReductionDecl::DirectInit)
1703
0
        Out << ")";
1704
56
      Out << ")";
1705
56
    }
1706
92
  }
1707
92
}
1708
1709
116
void DeclPrinter::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {
1710
116
  if (!D->isInvalidDecl()) {
1711
116
    Out << "#pragma omp declare mapper (";
1712
116
    D->printName(Out);
1713
116
    Out << " : ";
1714
116
    D->getType().print(Out, Policy);
1715
116
    Out << " ";
1716
116
    Out << D->getVarName();
1717
116
    Out << ")";
1718
116
    if (!D->clauselist_empty()) {
1719
116
      OMPClausePrinter Printer(Out, Policy);
1720
128
      for (auto *C : D->clauselists()) {
1721
128
        Out << " ";
1722
128
        Printer.Visit(C);
1723
128
      }
1724
116
    }
1725
116
  }
1726
116
}
1727
1728
0
void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
1729
0
  D->getInit()->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context);
1730
0
}
1731
1732
1.25k
void DeclPrinter::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *TTP) {
1733
1.25k
  if (const TypeConstraint *TC = TTP->getTypeConstraint())
1734
2
    TC->print(Out, Policy);
1735
1.25k
  else if (TTP->wasDeclaredWithTypename())
1736
797
    Out << "typename";
1737
459
  else
1738
459
    Out << "class";
1739
1740
1.25k
  if (TTP->isParameterPack())
1741
32
    Out << " ...";
1742
1.22k
  else if (TTP->getDeclName())
1743
1.20k
    Out << ' ';
1744
1745
1.25k
  if (TTP->getDeclName())
1746
1.23k
    Out << TTP->getDeclName();
1747
1748
1.25k
  if (TTP->hasDefaultArgument()) {
1749
7
    Out << " = ";
1750
7
    Out << TTP->getDefaultArgument().getAsString(Policy);
1751
7
  }
1752
1.25k
}
1753
1754
void DeclPrinter::VisitNonTypeTemplateParmDecl(
1755
382
    const NonTypeTemplateParmDecl *NTTP) {
1756
382
  StringRef Name;
1757
382
  if (IdentifierInfo *II = NTTP->getIdentifier())
1758
363
    Name = II->getName();
1759
382
  printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
1760
1761
382
  if (NTTP->hasDefaultArgument()) {
1762
9
    Out << " = ";
1763
9
    NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy, Indentation,
1764
9
                                            "\n", &Context);
1765
9
  }
1766
382
}