Coverage Report

Created: 2021-01-19 06:58

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