Coverage Report

Created: 2021-08-24 07:12

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