Coverage Report

Created: 2021-08-24 07:12

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/TypePrinter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- TypePrinter.cpp - Pretty-Print Clang Types -------------------------===//
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 contains code to print types from Clang's type system.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/AST/ASTContext.h"
14
#include "clang/AST/Attr.h"
15
#include "clang/AST/Decl.h"
16
#include "clang/AST/DeclBase.h"
17
#include "clang/AST/DeclCXX.h"
18
#include "clang/AST/DeclObjC.h"
19
#include "clang/AST/DeclTemplate.h"
20
#include "clang/AST/Expr.h"
21
#include "clang/AST/NestedNameSpecifier.h"
22
#include "clang/AST/PrettyPrinter.h"
23
#include "clang/AST/TemplateBase.h"
24
#include "clang/AST/TemplateName.h"
25
#include "clang/AST/Type.h"
26
#include "clang/Basic/AddressSpaces.h"
27
#include "clang/Basic/ExceptionSpecificationType.h"
28
#include "clang/Basic/IdentifierTable.h"
29
#include "clang/Basic/LLVM.h"
30
#include "clang/Basic/LangOptions.h"
31
#include "clang/Basic/SourceLocation.h"
32
#include "clang/Basic/SourceManager.h"
33
#include "clang/Basic/Specifiers.h"
34
#include "llvm/ADT/ArrayRef.h"
35
#include "llvm/ADT/SmallString.h"
36
#include "llvm/ADT/StringRef.h"
37
#include "llvm/ADT/Twine.h"
38
#include "llvm/Support/Casting.h"
39
#include "llvm/Support/Compiler.h"
40
#include "llvm/Support/ErrorHandling.h"
41
#include "llvm/Support/SaveAndRestore.h"
42
#include "llvm/Support/raw_ostream.h"
43
#include <cassert>
44
#include <string>
45
46
using namespace clang;
47
48
namespace {
49
50
  /// RAII object that enables printing of the ARC __strong lifetime
51
  /// qualifier.
52
  class IncludeStrongLifetimeRAII {
53
    PrintingPolicy &Policy;
54
    bool Old;
55
56
  public:
57
    explicit IncludeStrongLifetimeRAII(PrintingPolicy &Policy)
58
1.31M
        : Policy(Policy), Old(Policy.SuppressStrongLifetime) {
59
1.31M
        if (!Policy.SuppressLifetimeQualifiers)
60
1.31M
          Policy.SuppressStrongLifetime = false;
61
1.31M
    }
62
63
1.31M
    ~IncludeStrongLifetimeRAII() {
64
1.31M
      Policy.SuppressStrongLifetime = Old;
65
1.31M
    }
66
  };
67
68
  class ParamPolicyRAII {
69
    PrintingPolicy &Policy;
70
    bool Old;
71
72
  public:
73
    explicit ParamPolicyRAII(PrintingPolicy &Policy)
74
39.8k
        : Policy(Policy), Old(Policy.SuppressSpecifiers) {
75
39.8k
      Policy.SuppressSpecifiers = false;
76
39.8k
    }
77
78
39.8k
    ~ParamPolicyRAII() {
79
39.8k
      Policy.SuppressSpecifiers = Old;
80
39.8k
    }
81
  };
82
83
  class ElaboratedTypePolicyRAII {
84
    PrintingPolicy &Policy;
85
    bool SuppressTagKeyword;
86
    bool SuppressScope;
87
88
  public:
89
9.01k
    explicit ElaboratedTypePolicyRAII(PrintingPolicy &Policy) : Policy(Policy) {
90
9.01k
      SuppressTagKeyword = Policy.SuppressTagKeyword;
91
9.01k
      SuppressScope = Policy.SuppressScope;
92
9.01k
      Policy.SuppressTagKeyword = true;
93
9.01k
      Policy.SuppressScope = true;
94
9.01k
    }
95
96
9.01k
    ~ElaboratedTypePolicyRAII() {
97
9.01k
      Policy.SuppressTagKeyword = SuppressTagKeyword;
98
9.01k
      Policy.SuppressScope = SuppressScope;
99
9.01k
    }
100
  };
101
102
  class TypePrinter {
103
    PrintingPolicy Policy;
104
    unsigned Indentation;
105
    bool HasEmptyPlaceHolder = false;
106
    bool InsideCCAttribute = false;
107
108
  public:
109
    explicit TypePrinter(const PrintingPolicy &Policy, unsigned Indentation = 0)
110
2.15M
        : Policy(Policy), Indentation(Indentation) {}
111
112
    void print(const Type *ty, Qualifiers qs, raw_ostream &OS,
113
               StringRef PlaceHolder);
114
    void print(QualType T, raw_ostream &OS, StringRef PlaceHolder);
115
116
    static bool canPrefixQualifiers(const Type *T, bool &NeedARCStrongQualifier);
117
    void spaceBeforePlaceHolder(raw_ostream &OS);
118
    void printTypeSpec(NamedDecl *D, raw_ostream &OS);
119
    void printTemplateId(const TemplateSpecializationType *T, raw_ostream &OS,
120
                         bool FullyQualify);
121
122
    void printBefore(QualType T, raw_ostream &OS);
123
    void printAfter(QualType T, raw_ostream &OS);
124
    void AppendScope(DeclContext *DC, raw_ostream &OS,
125
                     DeclarationName NameInScope);
126
    void printTag(TagDecl *T, raw_ostream &OS);
127
    void printFunctionAfter(const FunctionType::ExtInfo &Info, raw_ostream &OS);
128
#define ABSTRACT_TYPE(CLASS, PARENT)
129
#define TYPE(CLASS, PARENT) \
130
    void print##CLASS##Before(const CLASS##Type *T, raw_ostream &OS); \
131
    void print##CLASS##After(const CLASS##Type *T, raw_ostream &OS);
132
#include "clang/AST/TypeNodes.inc"
133
134
  private:
135
    void printBefore(const Type *ty, Qualifiers qs, raw_ostream &OS);
136
    void printAfter(const Type *ty, Qualifiers qs, raw_ostream &OS);
137
  };
138
139
} // namespace
140
141
static void AppendTypeQualList(raw_ostream &OS, unsigned TypeQuals,
142
78.8k
                               bool HasRestrictKeyword) {
143
78.8k
  bool appendSpace = false;
144
78.8k
  if (TypeQuals & Qualifiers::Const) {
145
71.7k
    OS << "const";
146
71.7k
    appendSpace = true;
147
71.7k
  }
148
78.8k
  if (TypeQuals & Qualifiers::Volatile) {
149
7.98k
    if (appendSpace) 
OS << ' '1.09k
;
150
7.98k
    OS << "volatile";
151
7.98k
    appendSpace = true;
152
7.98k
  }
153
78.8k
  if (TypeQuals & Qualifiers::Restrict) {
154
2.15k
    if (appendSpace) 
OS << ' '1.95k
;
155
2.15k
    if (HasRestrictKeyword) {
156
1.85k
      OS << "restrict";
157
1.85k
    } else {
158
307
      OS << "__restrict";
159
307
    }
160
2.15k
  }
161
78.8k
}
162
163
2.19M
void TypePrinter::spaceBeforePlaceHolder(raw_ostream &OS) {
164
2.19M
  if (!HasEmptyPlaceHolder)
165
338k
    OS << ' ';
166
2.19M
}
167
168
static SplitQualType splitAccordingToPolicy(QualType QT,
169
3.05M
                                            const PrintingPolicy &Policy) {
170
3.05M
  if (Policy.PrintCanonicalTypes)
171
774k
    QT = QT.getCanonicalType();
172
3.05M
  return QT.split();
173
3.05M
}
174
175
39.2k
void TypePrinter::print(QualType t, raw_ostream &OS, StringRef PlaceHolder) {
176
39.2k
  SplitQualType split = splitAccordingToPolicy(t, Policy);
177
39.2k
  print(split.Ty, split.Quals, OS, PlaceHolder);
178
39.2k
}
179
180
void TypePrinter::print(const Type *T, Qualifiers Quals, raw_ostream &OS,
181
2.19M
                        StringRef PlaceHolder) {
182
2.19M
  if (!T) {
183
70
    OS << "NULL TYPE";
184
70
    return;
185
70
  }
186
187
2.19M
  SaveAndRestore<bool> PHVal(HasEmptyPlaceHolder, PlaceHolder.empty());
188
189
2.19M
  printBefore(T, Quals, OS);
190
2.19M
  OS << PlaceHolder;
191
2.19M
  printAfter(T, Quals, OS);
192
2.19M
}
193
194
bool TypePrinter::canPrefixQualifiers(const Type *T,
195
2.67M
                                      bool &NeedARCStrongQualifier) {
196
  // CanPrefixQualifiers - We prefer to print type qualifiers before the type,
197
  // so that we get "const int" instead of "int const", but we can't do this if
198
  // the type is complex.  For example if the type is "int*", we *must* print
199
  // "int * const", printing "const int *" is different.  Only do this when the
200
  // type expands to a simple string.
201
2.67M
  bool CanPrefixQualifiers = false;
202
2.67M
  NeedARCStrongQualifier = false;
203
2.67M
  Type::TypeClass TC = T->getTypeClass();
204
2.67M
  if (const auto *AT = dyn_cast<AutoType>(T))
205
2.98k
    TC = AT->desugar()->getTypeClass();
206
2.67M
  if (const auto *Subst = dyn_cast<SubstTemplateTypeParmType>(T))
207
17.5k
    TC = Subst->getReplacementType()->getTypeClass();
208
209
2.67M
  switch (TC) {
210
1.36k
    case Type::Auto:
211
967k
    case Type::Builtin:
212
968k
    case Type::Complex:
213
968k
    case Type::UnresolvedUsing:
214
1.00M
    case Type::Typedef:
215
1.00M
    case Type::TypeOfExpr:
216
1.00M
    case Type::TypeOf:
217
1.00M
    case Type::Decltype:
218
1.00M
    case Type::UnaryTransform:
219
1.85M
    case Type::Record:
220
1.86M
    case Type::Enum:
221
1.86M
    case Type::Elaborated:
222
2.05M
    case Type::TemplateTypeParm:
223
2.05M
    case Type::SubstTemplateTypeParmPack:
224
2.05M
    case Type::DeducedTemplateSpecialization:
225
2.05M
    case Type::TemplateSpecialization:
226
2.17M
    case Type::InjectedClassName:
227
2.17M
    case Type::DependentName:
228
2.17M
    case Type::DependentTemplateSpecialization:
229
2.18M
    case Type::ObjCObject:
230
2.18M
    case Type::ObjCTypeParam:
231
2.21M
    case Type::ObjCInterface:
232
2.21M
    case Type::Atomic:
233
2.21M
    case Type::Pipe:
234
2.21M
    case Type::ExtInt:
235
2.21M
    case Type::DependentExtInt:
236
2.21M
      CanPrefixQualifiers = true;
237
2.21M
      break;
238
239
26.1k
    case Type::ObjCObjectPointer:
240
26.1k
      CanPrefixQualifiers = T->isObjCIdType() || 
T->isObjCClassType()23.7k
||
241
26.1k
        
T->isObjCQualifiedIdType()23.3k
||
T->isObjCQualifiedClassType()22.1k
;
242
26.1k
      break;
243
244
11.5k
    case Type::ConstantArray:
245
13.0k
    case Type::IncompleteArray:
246
13.3k
    case Type::VariableArray:
247
13.4k
    case Type::DependentSizedArray:
248
13.4k
      NeedARCStrongQualifier = true;
249
13.4k
      LLVM_FALLTHROUGH;
250
251
13.4k
    case Type::Adjusted:
252
13.7k
    case Type::Decayed:
253
283k
    case Type::Pointer:
254
284k
    case Type::BlockPointer:
255
321k
    case Type::LValueReference:
256
329k
    case Type::RValueReference:
257
331k
    case Type::MemberPointer:
258
331k
    case Type::DependentAddressSpace:
259
331k
    case Type::DependentVector:
260
331k
    case Type::DependentSizedExtVector:
261
347k
    case Type::Vector:
262
351k
    case Type::ExtVector:
263
351k
    case Type::ConstantMatrix:
264
351k
    case Type::DependentSizedMatrix:
265
391k
    case Type::FunctionProto:
266
392k
    case Type::FunctionNoProto:
267
394k
    case Type::Paren:
268
432k
    case Type::PackExpansion:
269
432k
    case Type::SubstTemplateTypeParm:
270
432k
    case Type::MacroQualified:
271
432k
      CanPrefixQualifiers = false;
272
432k
      break;
273
274
1.48k
    case Type::Attributed: {
275
      // We still want to print the address_space before the type if it is an
276
      // address_space attribute.
277
1.48k
      const auto *AttrTy = cast<AttributedType>(T);
278
1.48k
      CanPrefixQualifiers = AttrTy->getAttrKind() == attr::AddressSpace;
279
1.48k
    }
280
2.67M
  }
281
282
2.67M
  return CanPrefixQualifiers;
283
2.67M
}
284
285
487k
void TypePrinter::printBefore(QualType T, raw_ostream &OS) {
286
487k
  SplitQualType Split = splitAccordingToPolicy(T, Policy);
287
288
  // If we have cv1 T, where T is substituted for cv2 U, only print cv1 - cv2
289
  // at this level.
290
487k
  Qualifiers Quals = Split.Quals;
291
487k
  if (const auto *Subst = dyn_cast<SubstTemplateTypeParmType>(Split.Ty))
292
2.48k
    Quals -= QualType(Subst, 0).getQualifiers();
293
294
487k
  printBefore(Split.Ty, Quals, OS);
295
487k
}
296
297
/// Prints the part of the type string before an identifier, e.g. for
298
/// "int foo[10]" it prints "int ".
299
2.68M
void TypePrinter::printBefore(const Type *T,Qualifiers Quals, raw_ostream &OS) {
300
2.68M
  if (Policy.SuppressSpecifiers && 
T->isSpecifierType()5.29k
)
301
4.97k
    return;
302
303
2.67M
  SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder);
304
305
  // Print qualifiers as appropriate.
306
307
2.67M
  bool CanPrefixQualifiers = false;
308
2.67M
  bool NeedARCStrongQualifier = false;
309
2.67M
  CanPrefixQualifiers = canPrefixQualifiers(T, NeedARCStrongQualifier);
310
311
2.67M
  if (CanPrefixQualifiers && 
!Quals.empty()2.22M
) {
312
73.8k
    if (NeedARCStrongQualifier) {
313
0
      IncludeStrongLifetimeRAII Strong(Policy);
314
0
      Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/true);
315
73.8k
    } else {
316
73.8k
      Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/true);
317
73.8k
    }
318
73.8k
  }
319
320
2.67M
  bool hasAfterQuals = false;
321
2.67M
  if (!CanPrefixQualifiers && 
!Quals.empty()455k
) {
322
14.5k
    hasAfterQuals = !Quals.isEmptyWhenPrinted(Policy);
323
14.5k
    if (hasAfterQuals)
324
14.4k
      HasEmptyPlaceHolder = false;
325
14.5k
  }
326
327
2.67M
  switch (T->getTypeClass()) {
328
0
#define ABSTRACT_TYPE(CLASS, PARENT)
329
2.67M
#define TYPE(CLASS, PARENT) case Type::CLASS: \
330
2.67M
    print##CLASS##Before(cast<CLASS##Type>(T), OS); \
331
2.67M
    break;
332
2.67M
#include 
"clang/AST/TypeNodes.inc"0
333
2.67M
  }
334
335
2.67M
  if (hasAfterQuals) {
336
14.4k
    if (NeedARCStrongQualifier) {
337
1.49k
      IncludeStrongLifetimeRAII Strong(Policy);
338
1.49k
      Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/!PrevPHIsEmpty.get());
339
12.9k
    } else {
340
12.9k
      Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/!PrevPHIsEmpty.get());
341
12.9k
    }
342
14.4k
  }
343
2.67M
}
344
345
459k
void TypePrinter::printAfter(QualType t, raw_ostream &OS) {
346
459k
  SplitQualType split = splitAccordingToPolicy(t, Policy);
347
459k
  printAfter(split.Ty, split.Quals, OS);
348
459k
}
349
350
/// Prints the part of the type string after an identifier, e.g. for
351
/// "int foo[10]" it prints "[10]".
352
2.65M
void TypePrinter::printAfter(const Type *T, Qualifiers Quals, raw_ostream &OS) {
353
2.65M
  switch (T->getTypeClass()) {
354
0
#define ABSTRACT_TYPE(CLASS, PARENT)
355
2.65M
#define TYPE(CLASS, PARENT) case Type::CLASS: \
356
2.65M
    print##CLASS##After(cast<CLASS##Type>(T), OS); \
357
2.65M
    break;
358
2.65M
#include 
"clang/AST/TypeNodes.inc"0
359
2.65M
  }
360
2.65M
}
361
362
954k
void TypePrinter::printBuiltinBefore(const BuiltinType *T, raw_ostream &OS) {
363
954k
  OS << T->getName(Policy);
364
954k
  spaceBeforePlaceHolder(OS);
365
954k
}
366
367
955k
void TypePrinter::printBuiltinAfter(const BuiltinType *T, raw_ostream &OS) {}
368
369
711
void TypePrinter::printComplexBefore(const ComplexType *T, raw_ostream &OS) {
370
711
  OS << "_Complex ";
371
711
  printBefore(T->getElementType(), OS);
372
711
}
373
374
713
void TypePrinter::printComplexAfter(const ComplexType *T, raw_ostream &OS) {
375
713
  printAfter(T->getElementType(), OS);
376
713
}
377
378
268k
void TypePrinter::printPointerBefore(const PointerType *T, raw_ostream &OS) {
379
268k
  IncludeStrongLifetimeRAII Strong(Policy);
380
268k
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
381
268k
  printBefore(T->getPointeeType(), OS);
382
  // Handle things like 'int (*A)[4];' correctly.
383
  // FIXME: this should include vectors, but vectors use attributes I guess.
384
268k
  if (isa<ArrayType>(T->getPointeeType()))
385
1.10k
    OS << '(';
386
268k
  OS << '*';
387
268k
}
388
389
268k
void TypePrinter::printPointerAfter(const PointerType *T, raw_ostream &OS) {
390
268k
  IncludeStrongLifetimeRAII Strong(Policy);
391
268k
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
392
  // Handle things like 'int (*A)[4];' correctly.
393
  // FIXME: this should include vectors, but vectors use attributes I guess.
394
268k
  if (isa<ArrayType>(T->getPointeeType()))
395
1.10k
    OS << ')';
396
268k
  printAfter(T->getPointeeType(), OS);
397
268k
}
398
399
void TypePrinter::printBlockPointerBefore(const BlockPointerType *T,
400
1.01k
                                          raw_ostream &OS) {
401
1.01k
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
402
1.01k
  printBefore(T->getPointeeType(), OS);
403
1.01k
  OS << '^';
404
1.01k
}
405
406
void TypePrinter::printBlockPointerAfter(const BlockPointerType *T,
407
1.01k
                                          raw_ostream &OS) {
408
1.01k
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
409
1.01k
  printAfter(T->getPointeeType(), OS);
410
1.01k
}
411
412
// When printing a reference, the referenced type might also be a reference.
413
// If so, we want to skip that before printing the inner type.
414
87.9k
static QualType skipTopLevelReferences(QualType T) {
415
87.9k
  if (auto *Ref = T->getAs<ReferenceType>())
416
384
    return skipTopLevelReferences(Ref->getPointeeTypeAsWritten());
417
87.5k
  return T;
418
87.9k
}
419
420
void TypePrinter::printLValueReferenceBefore(const LValueReferenceType *T,
421
36.3k
                                             raw_ostream &OS) {
422
36.3k
  IncludeStrongLifetimeRAII Strong(Policy);
423
36.3k
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
424
36.3k
  QualType Inner = skipTopLevelReferences(T->getPointeeTypeAsWritten());
425
36.3k
  printBefore(Inner, OS);
426
  // Handle things like 'int (&A)[4];' correctly.
427
  // FIXME: this should include vectors, but vectors use attributes I guess.
428
36.3k
  if (isa<ArrayType>(Inner))
429
929
    OS << '(';
430
36.3k
  OS << '&';
431
36.3k
}
432
433
void TypePrinter::printLValueReferenceAfter(const LValueReferenceType *T,
434
36.3k
                                            raw_ostream &OS) {
435
36.3k
  IncludeStrongLifetimeRAII Strong(Policy);
436
36.3k
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
437
36.3k
  QualType Inner = skipTopLevelReferences(T->getPointeeTypeAsWritten());
438
  // Handle things like 'int (&A)[4];' correctly.
439
  // FIXME: this should include vectors, but vectors use attributes I guess.
440
36.3k
  if (isa<ArrayType>(Inner))
441
929
    OS << ')';
442
36.3k
  printAfter(Inner, OS);
443
36.3k
}
444
445
void TypePrinter::printRValueReferenceBefore(const RValueReferenceType *T,
446
7.47k
                                             raw_ostream &OS) {
447
7.47k
  IncludeStrongLifetimeRAII Strong(Policy);
448
7.47k
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
449
7.47k
  QualType Inner = skipTopLevelReferences(T->getPointeeTypeAsWritten());
450
7.47k
  printBefore(Inner, OS);
451
  // Handle things like 'int (&&A)[4];' correctly.
452
  // FIXME: this should include vectors, but vectors use attributes I guess.
453
7.47k
  if (isa<ArrayType>(Inner))
454
7
    OS << '(';
455
7.47k
  OS << "&&";
456
7.47k
}
457
458
void TypePrinter::printRValueReferenceAfter(const RValueReferenceType *T,
459
7.47k
                                            raw_ostream &OS) {
460
7.47k
  IncludeStrongLifetimeRAII Strong(Policy);
461
7.47k
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
462
7.47k
  QualType Inner = skipTopLevelReferences(T->getPointeeTypeAsWritten());
463
  // Handle things like 'int (&&A)[4];' correctly.
464
  // FIXME: this should include vectors, but vectors use attributes I guess.
465
7.47k
  if (isa<ArrayType>(Inner))
466
7
    OS << ')';
467
7.47k
  printAfter(Inner, OS);
468
7.47k
}
469
470
void TypePrinter::printMemberPointerBefore(const MemberPointerType *T,
471
2.82k
                                           raw_ostream &OS) {
472
2.82k
  IncludeStrongLifetimeRAII Strong(Policy);
473
2.82k
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
474
2.82k
  printBefore(T->getPointeeType(), OS);
475
  // Handle things like 'int (Cls::*A)[4];' correctly.
476
  // FIXME: this should include vectors, but vectors use attributes I guess.
477
2.82k
  if (isa<ArrayType>(T->getPointeeType()))
478
0
    OS << '(';
479
480
2.82k
  PrintingPolicy InnerPolicy(Policy);
481
2.82k
  InnerPolicy.IncludeTagDefinition = false;
482
2.82k
  TypePrinter(InnerPolicy).print(QualType(T->getClass(), 0), OS, StringRef());
483
484
2.82k
  OS << "::*";
485
2.82k
}
486
487
void TypePrinter::printMemberPointerAfter(const MemberPointerType *T,
488
2.82k
                                          raw_ostream &OS) {
489
2.82k
  IncludeStrongLifetimeRAII Strong(Policy);
490
2.82k
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
491
  // Handle things like 'int (Cls::*A)[4];' correctly.
492
  // FIXME: this should include vectors, but vectors use attributes I guess.
493
2.82k
  if (isa<ArrayType>(T->getPointeeType()))
494
0
    OS << ')';
495
2.82k
  printAfter(T->getPointeeType(), OS);
496
2.82k
}
497
498
void TypePrinter::printConstantArrayBefore(const ConstantArrayType *T,
499
11.4k
                                           raw_ostream &OS) {
500
11.4k
  IncludeStrongLifetimeRAII Strong(Policy);
501
11.4k
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
502
11.4k
  printBefore(T->getElementType(), OS);
503
11.4k
}
504
505
void TypePrinter::printConstantArrayAfter(const ConstantArrayType *T,
506
11.4k
                                          raw_ostream &OS) {
507
11.4k
  OS << '[';
508
11.4k
  if (T->getIndexTypeQualifiers().hasQualifiers()) {
509
1
    AppendTypeQualList(OS, T->getIndexTypeCVRQualifiers(),
510
1
                       Policy.Restrict);
511
1
    OS << ' ';
512
1
  }
513
514
11.4k
  if (T->getSizeModifier() == ArrayType::Static)
515
2
    OS << "static ";
516
517
11.4k
  OS << T->getSize().getZExtValue() << ']';
518
11.4k
  printAfter(T->getElementType(), OS);
519
11.4k
}
520
521
void TypePrinter::printIncompleteArrayBefore(const IncompleteArrayType *T,
522
1.55k
                                             raw_ostream &OS) {
523
1.55k
  IncludeStrongLifetimeRAII Strong(Policy);
524
1.55k
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
525
1.55k
  printBefore(T->getElementType(), OS);
526
1.55k
}
527
528
void TypePrinter::printIncompleteArrayAfter(const IncompleteArrayType *T,
529
1.55k
                                            raw_ostream &OS) {
530
1.55k
  OS << "[]";
531
1.55k
  printAfter(T->getElementType(), OS);
532
1.55k
}
533
534
void TypePrinter::printVariableArrayBefore(const VariableArrayType *T,
535
238
                                           raw_ostream &OS) {
536
238
  IncludeStrongLifetimeRAII Strong(Policy);
537
238
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
538
238
  printBefore(T->getElementType(), OS);
539
238
}
540
541
void TypePrinter::printVariableArrayAfter(const VariableArrayType *T,
542
238
                                          raw_ostream &OS) {
543
238
  OS << '[';
544
238
  if (T->getIndexTypeQualifiers().hasQualifiers()) {
545
1
    AppendTypeQualList(OS, T->getIndexTypeCVRQualifiers(), Policy.Restrict);
546
1
    OS << ' ';
547
1
  }
548
549
238
  if (T->getSizeModifier() == VariableArrayType::Static)
550
2
    OS << "static ";
551
236
  else if (T->getSizeModifier() == VariableArrayType::Star)
552
8
    OS << '*';
553
554
238
  if (T->getSizeExpr())
555
230
    T->getSizeExpr()->printPretty(OS, nullptr, Policy);
556
238
  OS << ']';
557
558
238
  printAfter(T->getElementType(), OS);
559
238
}
560
561
271
void TypePrinter::printAdjustedBefore(const AdjustedType *T, raw_ostream &OS) {
562
  // Print the adjusted representation, otherwise the adjustment will be
563
  // invisible.
564
271
  printBefore(T->getAdjustedType(), OS);
565
271
}
566
567
271
void TypePrinter::printAdjustedAfter(const AdjustedType *T, raw_ostream &OS) {
568
271
  printAfter(T->getAdjustedType(), OS);
569
271
}
570
571
271
void TypePrinter::printDecayedBefore(const DecayedType *T, raw_ostream &OS) {
572
  // Print as though it's a pointer.
573
271
  printAdjustedBefore(T, OS);
574
271
}
575
576
271
void TypePrinter::printDecayedAfter(const DecayedType *T, raw_ostream &OS) {
577
271
  printAdjustedAfter(T, OS);
578
271
}
579
580
void TypePrinter::printDependentSizedArrayBefore(
581
                                               const DependentSizedArrayType *T,
582
166
                                               raw_ostream &OS) {
583
166
  IncludeStrongLifetimeRAII Strong(Policy);
584
166
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
585
166
  printBefore(T->getElementType(), OS);
586
166
}
587
588
void TypePrinter::printDependentSizedArrayAfter(
589
                                               const DependentSizedArrayType *T,
590
166
                                               raw_ostream &OS) {
591
166
  OS << '[';
592
166
  if (T->getSizeExpr())
593
166
    T->getSizeExpr()->printPretty(OS, nullptr, Policy);
594
166
  OS << ']';
595
166
  printAfter(T->getElementType(), OS);
596
166
}
597
598
void TypePrinter::printDependentAddressSpaceBefore(
599
2
    const DependentAddressSpaceType *T, raw_ostream &OS) {
600
2
  printBefore(T->getPointeeType(), OS);
601
2
}
602
603
void TypePrinter::printDependentAddressSpaceAfter(
604
2
    const DependentAddressSpaceType *T, raw_ostream &OS) {
605
2
  OS << " __attribute__((address_space(";
606
2
  if (T->getAddrSpaceExpr())
607
2
    T->getAddrSpaceExpr()->printPretty(OS, nullptr, Policy);
608
2
  OS << ")))";
609
2
  printAfter(T->getPointeeType(), OS);
610
2
}
611
612
void TypePrinter::printDependentSizedExtVectorBefore(
613
                                          const DependentSizedExtVectorType *T,
614
0
                                          raw_ostream &OS) {
615
0
  printBefore(T->getElementType(), OS);
616
0
}
617
618
void TypePrinter::printDependentSizedExtVectorAfter(
619
                                          const DependentSizedExtVectorType *T,
620
0
                                          raw_ostream &OS) {
621
0
  OS << " __attribute__((ext_vector_type(";
622
0
  if (T->getSizeExpr())
623
0
    T->getSizeExpr()->printPretty(OS, nullptr, Policy);
624
0
  OS << ")))";
625
0
  printAfter(T->getElementType(), OS);
626
0
}
627
628
15.8k
void TypePrinter::printVectorBefore(const VectorType *T, raw_ostream &OS) {
629
15.8k
  switch (T->getVectorKind()) {
630
52
  case VectorType::AltiVecPixel:
631
52
    OS << "__vector __pixel ";
632
52
    break;
633
3.01k
  case VectorType::AltiVecBool:
634
3.01k
    OS << "__vector __bool ";
635
3.01k
    printBefore(T->getElementType(), OS);
636
3.01k
    break;
637
11.5k
  case VectorType::AltiVecVector:
638
11.5k
    OS << "__vector ";
639
11.5k
    printBefore(T->getElementType(), OS);
640
11.5k
    break;
641
124
  case VectorType::NeonVector:
642
124
    OS << "__attribute__((neon_vector_type("
643
124
       << T->getNumElements() << "))) ";
644
124
    printBefore(T->getElementType(), OS);
645
124
    break;
646
0
  case VectorType::NeonPolyVector:
647
0
    OS << "__attribute__((neon_polyvector_type(" <<
648
0
          T->getNumElements() << "))) ";
649
0
    printBefore(T->getElementType(), OS);
650
0
    break;
651
1.02k
  case VectorType::GenericVector: {
652
    // FIXME: We prefer to print the size directly here, but have no way
653
    // to get the size of the type.
654
1.02k
    OS << "__attribute__((__vector_size__("
655
1.02k
       << T->getNumElements()
656
1.02k
       << " * sizeof(";
657
1.02k
    print(T->getElementType(), OS, StringRef());
658
1.02k
    OS << ")))) ";
659
1.02k
    printBefore(T->getElementType(), OS);
660
1.02k
    break;
661
0
  }
662
118
  case VectorType::SveFixedLengthDataVector:
663
134
  case VectorType::SveFixedLengthPredicateVector:
664
    // FIXME: We prefer to print the size directly here, but have no way
665
    // to get the size of the type.
666
134
    OS << "__attribute__((__arm_sve_vector_bits__(";
667
668
134
    if (T->getVectorKind() == VectorType::SveFixedLengthPredicateVector)
669
      // Predicates take a bit per byte of the vector size, multiply by 8 to
670
      // get the number of bits passed to the attribute.
671
16
      OS << T->getNumElements() * 8;
672
118
    else
673
118
      OS << T->getNumElements();
674
675
134
    OS << " * sizeof(";
676
134
    print(T->getElementType(), OS, StringRef());
677
    // Multiply by 8 for the number of bits.
678
134
    OS << ") * 8))) ";
679
134
    printBefore(T->getElementType(), OS);
680
15.8k
  }
681
15.8k
}
682
683
15.8k
void TypePrinter::printVectorAfter(const VectorType *T, raw_ostream &OS) {
684
15.8k
  printAfter(T->getElementType(), OS);
685
15.8k
}
686
687
void TypePrinter::printDependentVectorBefore(
688
0
    const DependentVectorType *T, raw_ostream &OS) {
689
0
  switch (T->getVectorKind()) {
690
0
  case VectorType::AltiVecPixel:
691
0
    OS << "__vector __pixel ";
692
0
    break;
693
0
  case VectorType::AltiVecBool:
694
0
    OS << "__vector __bool ";
695
0
    printBefore(T->getElementType(), OS);
696
0
    break;
697
0
  case VectorType::AltiVecVector:
698
0
    OS << "__vector ";
699
0
    printBefore(T->getElementType(), OS);
700
0
    break;
701
0
  case VectorType::NeonVector:
702
0
    OS << "__attribute__((neon_vector_type(";
703
0
    if (T->getSizeExpr())
704
0
      T->getSizeExpr()->printPretty(OS, nullptr, Policy);
705
0
    OS << "))) ";
706
0
    printBefore(T->getElementType(), OS);
707
0
    break;
708
0
  case VectorType::NeonPolyVector:
709
0
    OS << "__attribute__((neon_polyvector_type(";
710
0
    if (T->getSizeExpr())
711
0
      T->getSizeExpr()->printPretty(OS, nullptr, Policy);
712
0
    OS << "))) ";
713
0
    printBefore(T->getElementType(), OS);
714
0
    break;
715
0
  case VectorType::GenericVector: {
716
    // FIXME: We prefer to print the size directly here, but have no way
717
    // to get the size of the type.
718
0
    OS << "__attribute__((__vector_size__(";
719
0
    if (T->getSizeExpr())
720
0
      T->getSizeExpr()->printPretty(OS, nullptr, Policy);
721
0
    OS << " * sizeof(";
722
0
    print(T->getElementType(), OS, StringRef());
723
0
    OS << ")))) ";
724
0
    printBefore(T->getElementType(), OS);
725
0
    break;
726
0
  }
727
0
  case VectorType::SveFixedLengthDataVector:
728
0
  case VectorType::SveFixedLengthPredicateVector:
729
    // FIXME: We prefer to print the size directly here, but have no way
730
    // to get the size of the type.
731
0
    OS << "__attribute__((__arm_sve_vector_bits__(";
732
0
    if (T->getSizeExpr()) {
733
0
      T->getSizeExpr()->printPretty(OS, nullptr, Policy);
734
0
      if (T->getVectorKind() == VectorType::SveFixedLengthPredicateVector)
735
        // Predicates take a bit per byte of the vector size, multiply by 8 to
736
        // get the number of bits passed to the attribute.
737
0
        OS << " * 8";
738
0
      OS << " * sizeof(";
739
0
      print(T->getElementType(), OS, StringRef());
740
      // Multiply by 8 for the number of bits.
741
0
      OS << ") * 8";
742
0
    }
743
0
    OS << "))) ";
744
0
    printBefore(T->getElementType(), OS);
745
0
  }
746
0
}
747
748
void TypePrinter::printDependentVectorAfter(
749
0
    const DependentVectorType *T, raw_ostream &OS) {
750
0
  printAfter(T->getElementType(), OS);
751
0
}
752
753
void TypePrinter::printExtVectorBefore(const ExtVectorType *T,
754
3.28k
                                       raw_ostream &OS) {
755
3.28k
  printBefore(T->getElementType(), OS);
756
3.28k
}
757
758
3.28k
void TypePrinter::printExtVectorAfter(const ExtVectorType *T, raw_ostream &OS) {
759
3.28k
  printAfter(T->getElementType(), OS);
760
3.28k
  OS << " __attribute__((ext_vector_type(";
761
3.28k
  OS << T->getNumElements();
762
3.28k
  OS << ")))";
763
3.28k
}
764
765
void TypePrinter::printConstantMatrixBefore(const ConstantMatrixType *T,
766
609
                                            raw_ostream &OS) {
767
609
  printBefore(T->getElementType(), OS);
768
609
  OS << " __attribute__((matrix_type(";
769
609
  OS << T->getNumRows() << ", " << T->getNumColumns();
770
609
  OS << ")))";
771
609
}
772
773
void TypePrinter::printConstantMatrixAfter(const ConstantMatrixType *T,
774
609
                                           raw_ostream &OS) {
775
609
  printAfter(T->getElementType(), OS);
776
609
}
777
778
void TypePrinter::printDependentSizedMatrixBefore(
779
0
    const DependentSizedMatrixType *T, raw_ostream &OS) {
780
0
  printBefore(T->getElementType(), OS);
781
0
  OS << " __attribute__((matrix_type(";
782
0
  if (T->getRowExpr()) {
783
0
    T->getRowExpr()->printPretty(OS, nullptr, Policy);
784
0
  }
785
0
  OS << ", ";
786
0
  if (T->getColumnExpr()) {
787
0
    T->getColumnExpr()->printPretty(OS, nullptr, Policy);
788
0
  }
789
0
  OS << ")))";
790
0
}
791
792
void TypePrinter::printDependentSizedMatrixAfter(
793
0
    const DependentSizedMatrixType *T, raw_ostream &OS) {
794
0
  printAfter(T->getElementType(), OS);
795
0
}
796
797
void
798
FunctionProtoType::printExceptionSpecification(raw_ostream &OS,
799
                                               const PrintingPolicy &Policy)
800
39.9k
                                                                         const {
801
39.9k
  if (hasDynamicExceptionSpec()) {
802
147
    OS << " throw(";
803
147
    if (getExceptionSpecType() == EST_MSAny)
804
0
      OS << "...";
805
147
    else
806
200
      
for (unsigned I = 0, N = getNumExceptions(); 147
I != N;
++I53
) {
807
53
        if (I)
808
9
          OS << ", ";
809
810
53
        OS << getExceptionType(I).stream(Policy);
811
53
      }
812
147
    OS << ')';
813
39.8k
  } else if (EST_NoThrow == getExceptionSpecType()) {
814
39
    OS << " __attribute__((nothrow))";
815
39.7k
  } else if (isNoexceptExceptionSpec(getExceptionSpecType())) {
816
1.45k
    OS << " noexcept";
817
    // FIXME:Is it useful to print out the expression for a non-dependent
818
    // noexcept specification?
819
1.45k
    if (isComputedNoexcept(getExceptionSpecType())) {
820
29
      OS << '(';
821
29
      if (getNoexceptExpr())
822
29
        getNoexceptExpr()->printPretty(OS, nullptr, Policy);
823
29
      OS << ')';
824
29
    }
825
1.45k
  }
826
39.9k
}
827
828
void TypePrinter::printFunctionProtoBefore(const FunctionProtoType *T,
829
39.8k
                                           raw_ostream &OS) {
830
39.8k
  if (T->hasTrailingReturn()) {
831
176
    OS << "auto ";
832
176
    if (!HasEmptyPlaceHolder)
833
35
      OS << '(';
834
39.7k
  } else {
835
    // If needed for precedence reasons, wrap the inner part in grouping parens.
836
39.7k
    SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false);
837
39.7k
    printBefore(T->getReturnType(), OS);
838
39.7k
    if (!PrevPHIsEmpty.get())
839
24.4k
      OS << '(';
840
39.7k
  }
841
39.8k
}
842
843
31
StringRef clang::getParameterABISpelling(ParameterABI ABI) {
844
31
  switch (ABI) {
845
0
  case ParameterABI::Ordinary:
846
0
    llvm_unreachable("asking for spelling of ordinary parameter ABI");
847
6
  case ParameterABI::SwiftContext:
848
6
    return "swift_context";
849
8
  case ParameterABI::SwiftAsyncContext:
850
8
    return "swift_async_context";
851
9
  case ParameterABI::SwiftErrorResult:
852
9
    return "swift_error_result";
853
8
  case ParameterABI::SwiftIndirectResult:
854
8
    return "swift_indirect_result";
855
31
  }
856
0
  llvm_unreachable("bad parameter ABI kind");
857
0
}
858
859
void TypePrinter::printFunctionProtoAfter(const FunctionProtoType *T,
860
39.8k
                                          raw_ostream &OS) {
861
  // If needed for precedence reasons, wrap the inner part in grouping parens.
862
39.8k
  if (!HasEmptyPlaceHolder)
863
24.4k
    OS << ')';
864
39.8k
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
865
866
39.8k
  OS << '(';
867
39.8k
  {
868
39.8k
    ParamPolicyRAII ParamPolicy(Policy);
869
71.2k
    for (unsigned i = 0, e = T->getNumParams(); i != e; 
++i31.3k
) {
870
31.3k
      if (i) 
OS << ", "6.11k
;
871
872
31.3k
      auto EPI = T->getExtParameterInfo(i);
873
31.3k
      if (EPI.isConsumed()) 
OS << "__attribute__((ns_consumed)) "159
;
874
31.3k
      if (EPI.isNoEscape())
875
38
        OS << "__attribute__((noescape)) ";
876
31.3k
      auto ABI = EPI.getABI();
877
31.3k
      if (ABI != ParameterABI::Ordinary)
878
0
        OS << "__attribute__((" << getParameterABISpelling(ABI) << ")) ";
879
880
31.3k
      print(T->getParamType(i), OS, StringRef());
881
31.3k
    }
882
39.8k
  }
883
884
39.8k
  if (T->isVariadic()) {
885
529
    if (T->getNumParams())
886
416
      OS << ", ";
887
529
    OS << "...";
888
39.3k
  } else if (T->getNumParams() == 0 && 
Policy.UseVoidForZeroParams14.5k
) {
889
    // Do not emit int() if we have a proto, emit 'int(void)'.
890
1.49k
    OS << "void";
891
1.49k
  }
892
893
39.8k
  OS << ')';
894
895
39.8k
  FunctionType::ExtInfo Info = T->getExtInfo();
896
897
39.8k
  printFunctionAfter(Info, OS);
898
899
39.8k
  if (!T->getMethodQuals().empty())
900
1.70k
    OS << " " << T->getMethodQuals().getAsString();
901
902
39.8k
  switch (T->getRefQualifier()) {
903
39.8k
  case RQ_None:
904
39.8k
    break;
905
906
47
  case RQ_LValue:
907
47
    OS << " &";
908
47
    break;
909
910
34
  case RQ_RValue:
911
34
    OS << " &&";
912
34
    break;
913
39.8k
  }
914
39.8k
  T->printExceptionSpecification(OS, Policy);
915
916
39.8k
  if (T->hasTrailingReturn()) {
917
176
    OS << " -> ";
918
176
    print(T->getReturnType(), OS, StringRef());
919
176
  } else
920
39.7k
    printAfter(T->getReturnType(), OS);
921
39.8k
}
922
923
void TypePrinter::printFunctionAfter(const FunctionType::ExtInfo &Info,
924
40.5k
                                     raw_ostream &OS) {
925
40.5k
  if (!InsideCCAttribute) {
926
40.2k
    switch (Info.getCC()) {
927
39.2k
    case CC_C:
928
      // The C calling convention is the default on the vast majority of platforms
929
      // we support.  If the user wrote it explicitly, it will usually be printed
930
      // while traversing the AttributedType.  If the type has been desugared, let
931
      // the canonical spelling be the implicit calling convention.
932
      // FIXME: It would be better to be explicit in certain contexts, such as a
933
      // cdecl function typedef used to declare a member function with the
934
      // Microsoft C++ ABI.
935
39.2k
      break;
936
49
    case CC_X86StdCall:
937
49
      OS << " __attribute__((stdcall))";
938
49
      break;
939
42
    case CC_X86FastCall:
940
42
      OS << " __attribute__((fastcall))";
941
42
      break;
942
530
    case CC_X86ThisCall:
943
530
      OS << " __attribute__((thiscall))";
944
530
      break;
945
277
    case CC_X86VectorCall:
946
277
      OS << " __attribute__((vectorcall))";
947
277
      break;
948
0
    case CC_X86Pascal:
949
0
      OS << " __attribute__((pascal))";
950
0
      break;
951
0
    case CC_AAPCS:
952
0
      OS << " __attribute__((pcs(\"aapcs\")))";
953
0
      break;
954
0
    case CC_AAPCS_VFP:
955
0
      OS << " __attribute__((pcs(\"aapcs-vfp\")))";
956
0
      break;
957
3
    case CC_AArch64VectorCall:
958
3
      OS << "__attribute__((aarch64_vector_pcs))";
959
3
      break;
960
0
    case CC_IntelOclBicc:
961
0
      OS << " __attribute__((intel_ocl_bicc))";
962
0
      break;
963
4
    case CC_Win64:
964
4
      OS << " __attribute__((ms_abi))";
965
4
      break;
966
4
    case CC_X86_64SysV:
967
4
      OS << " __attribute__((sysv_abi))";
968
4
      break;
969
2
    case CC_X86RegCall:
970
2
      OS << " __attribute__((regcall))";
971
2
      break;
972
11
    case CC_SpirFunction:
973
98
    case CC_OpenCLKernel:
974
      // Do nothing. These CCs are not available as attributes.
975
98
      break;
976
0
    case CC_Swift:
977
0
      OS << " __attribute__((swiftcall))";
978
0
      break;
979
0
    case CC_SwiftAsync:
980
0
      OS << "__attribute__((swiftasynccall))";
981
0
      break;
982
20
    case CC_PreserveMost:
983
20
      OS << " __attribute__((preserve_most))";
984
20
      break;
985
20
    case CC_PreserveAll:
986
20
      OS << " __attribute__((preserve_all))";
987
20
      break;
988
40.2k
    }
989
40.2k
  }
990
991
40.5k
  if (Info.getNoReturn())
992
73
    OS << " __attribute__((noreturn))";
993
40.5k
  if (Info.getCmseNSCall())
994
18
    OS << " __attribute__((cmse_nonsecure_call))";
995
40.5k
  if (Info.getProducesResult())
996
5
    OS << " __attribute__((ns_returns_retained))";
997
40.5k
  if (Info.getRegParm())
998
0
    OS << " __attribute__((regparm ("
999
0
       << Info.getRegParm() << ")))";
1000
40.5k
  if (Info.getNoCallerSavedRegs())
1001
4
    OS << " __attribute__((no_caller_saved_registers))";
1002
40.5k
  if (Info.getNoCfCheck())
1003
22
    OS << " __attribute__((nocf_check))";
1004
40.5k
}
1005
1006
void TypePrinter::printFunctionNoProtoBefore(const FunctionNoProtoType *T,
1007
666
                                             raw_ostream &OS) {
1008
  // If needed for precedence reasons, wrap the inner part in grouping parens.
1009
666
  SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false);
1010
666
  printBefore(T->getReturnType(), OS);
1011
666
  if (!PrevPHIsEmpty.get())
1012
245
    OS << '(';
1013
666
}
1014
1015
void TypePrinter::printFunctionNoProtoAfter(const FunctionNoProtoType *T,
1016
666
                                            raw_ostream &OS) {
1017
  // If needed for precedence reasons, wrap the inner part in grouping parens.
1018
666
  if (!HasEmptyPlaceHolder)
1019
245
    OS << ')';
1020
666
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
1021
1022
666
  OS << "()";
1023
666
  printFunctionAfter(T->getExtInfo(), OS);
1024
666
  printAfter(T->getReturnType(), OS);
1025
666
}
1026
1027
54.2k
void TypePrinter::printTypeSpec(NamedDecl *D, raw_ostream &OS) {
1028
1029
  // Compute the full nested-name-specifier for this type.
1030
  // In C, this will always be empty except when the type
1031
  // being printed is anonymous within other Record.
1032
54.2k
  if (!Policy.SuppressScope)
1033
52.2k
    AppendScope(D->getDeclContext(), OS, D->getDeclName());
1034
1035
54.2k
  IdentifierInfo *II = D->getIdentifier();
1036
54.2k
  OS << II->getName();
1037
54.2k
  spaceBeforePlaceHolder(OS);
1038
54.2k
}
1039
1040
void TypePrinter::printUnresolvedUsingBefore(const UnresolvedUsingType *T,
1041
0
                                             raw_ostream &OS) {
1042
0
  printTypeSpec(T->getDecl(), OS);
1043
0
}
1044
1045
void TypePrinter::printUnresolvedUsingAfter(const UnresolvedUsingType *T,
1046
0
                                            raw_ostream &OS) {}
1047
1048
38.1k
void TypePrinter::printTypedefBefore(const TypedefType *T, raw_ostream &OS) {
1049
38.1k
  printTypeSpec(T->getDecl(), OS);
1050
38.1k
}
1051
1052
void TypePrinter::printMacroQualifiedBefore(const MacroQualifiedType *T,
1053
37
                                            raw_ostream &OS) {
1054
37
  StringRef MacroName = T->getMacroIdentifier()->getName();
1055
37
  OS << MacroName << " ";
1056
1057
  // Since this type is meant to print the macro instead of the whole attribute,
1058
  // we trim any attributes and go directly to the original modified type.
1059
37
  printBefore(T->getModifiedType(), OS);
1060
37
}
1061
1062
void TypePrinter::printMacroQualifiedAfter(const MacroQualifiedType *T,
1063
37
                                           raw_ostream &OS) {
1064
37
  printAfter(T->getModifiedType(), OS);
1065
37
}
1066
1067
38.1k
void TypePrinter::printTypedefAfter(const TypedefType *T, raw_ostream &OS) {}
1068
1069
void TypePrinter::printTypeOfExprBefore(const TypeOfExprType *T,
1070
88
                                        raw_ostream &OS) {
1071
88
  OS << "typeof ";
1072
88
  if (T->getUnderlyingExpr())
1073
88
    T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy);
1074
88
  spaceBeforePlaceHolder(OS);
1075
88
}
1076
1077
void TypePrinter::printTypeOfExprAfter(const TypeOfExprType *T,
1078
88
                                       raw_ostream &OS) {}
1079
1080
5
void TypePrinter::printTypeOfBefore(const TypeOfType *T, raw_ostream &OS) {
1081
5
  OS << "typeof(";
1082
5
  print(T->getUnderlyingType(), OS, StringRef());
1083
5
  OS << ')';
1084
5
  spaceBeforePlaceHolder(OS);
1085
5
}
1086
1087
5
void TypePrinter::printTypeOfAfter(const TypeOfType *T, raw_ostream &OS) {}
1088
1089
254
void TypePrinter::printDecltypeBefore(const DecltypeType *T, raw_ostream &OS) {
1090
254
  OS << "decltype(";
1091
254
  if (T->getUnderlyingExpr())
1092
254
    T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy);
1093
254
  OS << ')';
1094
254
  spaceBeforePlaceHolder(OS);
1095
254
}
1096
1097
254
void TypePrinter::printDecltypeAfter(const DecltypeType *T, raw_ostream &OS) {}
1098
1099
void TypePrinter::printUnaryTransformBefore(const UnaryTransformType *T,
1100
4
                                            raw_ostream &OS) {
1101
4
  IncludeStrongLifetimeRAII Strong(Policy);
1102
1103
4
  switch (T->getUTTKind()) {
1104
4
    case UnaryTransformType::EnumUnderlyingType:
1105
4
      OS << "__underlying_type(";
1106
4
      print(T->getBaseType(), OS, StringRef());
1107
4
      OS << ')';
1108
4
      spaceBeforePlaceHolder(OS);
1109
4
      return;
1110
4
  }
1111
1112
0
  printBefore(T->getBaseType(), OS);
1113
0
}
1114
1115
void TypePrinter::printUnaryTransformAfter(const UnaryTransformType *T,
1116
4
                                           raw_ostream &OS) {
1117
4
  IncludeStrongLifetimeRAII Strong(Policy);
1118
1119
4
  switch (T->getUTTKind()) {
1120
4
    case UnaryTransformType::EnumUnderlyingType:
1121
4
      return;
1122
4
  }
1123
1124
0
  printAfter(T->getBaseType(), OS);
1125
0
}
1126
1127
2.98k
void TypePrinter::printAutoBefore(const AutoType *T, raw_ostream &OS) {
1128
  // If the type has been deduced, do not print 'auto'.
1129
2.98k
  if (!T->getDeducedType().isNull()) {
1130
1.62k
    printBefore(T->getDeducedType(), OS);
1131
1.62k
  } else {
1132
1.36k
    if (T->isConstrained()) {
1133
      // FIXME: Track a TypeConstraint as type sugar, so that we can print the
1134
      // type as it was written.
1135
8
      T->getTypeConstraintConcept()->getDeclName().print(OS, Policy);
1136
8
      auto Args = T->getTypeConstraintArguments();
1137
8
      if (!Args.empty())
1138
2
        printTemplateArgumentList(
1139
2
            OS, Args, Policy,
1140
2
            T->getTypeConstraintConcept()->getTemplateParameters());
1141
8
      OS << ' ';
1142
8
    }
1143
1.36k
    switch (T->getKeyword()) {
1144
1.29k
    case AutoTypeKeyword::Auto: OS << "auto"; break;
1145
59
    case AutoTypeKeyword::DecltypeAuto: OS << "decltype(auto)"; break;
1146
4
    case AutoTypeKeyword::GNUAutoType: OS << "__auto_type"; break;
1147
1.36k
    }
1148
1.36k
    spaceBeforePlaceHolder(OS);
1149
1.36k
  }
1150
2.98k
}
1151
1152
2.98k
void TypePrinter::printAutoAfter(const AutoType *T, raw_ostream &OS) {
1153
  // If the type has been deduced, do not print 'auto'.
1154
2.98k
  if (!T->getDeducedType().isNull())
1155
1.62k
    printAfter(T->getDeducedType(), OS);
1156
2.98k
}
1157
1158
void TypePrinter::printDeducedTemplateSpecializationBefore(
1159
162
    const DeducedTemplateSpecializationType *T, raw_ostream &OS) {
1160
  // If the type has been deduced, print the deduced type.
1161
162
  if (!T->getDeducedType().isNull()) {
1162
48
    printBefore(T->getDeducedType(), OS);
1163
114
  } else {
1164
114
    IncludeStrongLifetimeRAII Strong(Policy);
1165
114
    T->getTemplateName().print(OS, Policy);
1166
114
    spaceBeforePlaceHolder(OS);
1167
114
  }
1168
162
}
1169
1170
void TypePrinter::printDeducedTemplateSpecializationAfter(
1171
162
    const DeducedTemplateSpecializationType *T, raw_ostream &OS) {
1172
  // If the type has been deduced, print the deduced type.
1173
162
  if (!T->getDeducedType().isNull())
1174
48
    printAfter(T->getDeducedType(), OS);
1175
162
}
1176
1177
608
void TypePrinter::printAtomicBefore(const AtomicType *T, raw_ostream &OS) {
1178
608
  IncludeStrongLifetimeRAII Strong(Policy);
1179
1180
608
  OS << "_Atomic(";
1181
608
  print(T->getValueType(), OS, StringRef());
1182
608
  OS << ')';
1183
608
  spaceBeforePlaceHolder(OS);
1184
608
}
1185
1186
608
void TypePrinter::printAtomicAfter(const AtomicType *T, raw_ostream &OS) {}
1187
1188
146
void TypePrinter::printPipeBefore(const PipeType *T, raw_ostream &OS) {
1189
146
  IncludeStrongLifetimeRAII Strong(Policy);
1190
1191
146
  if (T->isReadOnly())
1192
123
    OS << "read_only ";
1193
23
  else
1194
23
    OS << "write_only ";
1195
146
  OS << "pipe ";
1196
146
  print(T->getElementType(), OS, StringRef());
1197
146
  spaceBeforePlaceHolder(OS);
1198
146
}
1199
1200
146
void TypePrinter::printPipeAfter(const PipeType *T, raw_ostream &OS) {}
1201
1202
68
void TypePrinter::printExtIntBefore(const ExtIntType *T, raw_ostream &OS) {
1203
68
  if (T->isUnsigned())
1204
10
    OS << "unsigned ";
1205
68
  OS << "_ExtInt(" << T->getNumBits() << ")";
1206
68
  spaceBeforePlaceHolder(OS);
1207
68
}
1208
1209
68
void TypePrinter::printExtIntAfter(const ExtIntType *T, raw_ostream &OS) {}
1210
1211
void TypePrinter::printDependentExtIntBefore(const DependentExtIntType *T,
1212
0
                                             raw_ostream &OS) {
1213
0
  if (T->isUnsigned())
1214
0
    OS << "unsigned ";
1215
0
  OS << "_ExtInt(";
1216
0
  T->getNumBitsExpr()->printPretty(OS, nullptr, Policy);
1217
0
  OS << ")";
1218
0
  spaceBeforePlaceHolder(OS);
1219
0
}
1220
1221
void TypePrinter::printDependentExtIntAfter(const DependentExtIntType *T,
1222
0
                                            raw_ostream &OS) {}
1223
1224
/// Appends the given scope to the end of a string.
1225
void TypePrinter::AppendScope(DeclContext *DC, raw_ostream &OS,
1226
2.15M
                              DeclarationName NameInScope) {
1227
2.15M
  if (DC->isTranslationUnit())
1228
861k
    return;
1229
1230
  // FIXME: Consider replacing this with NamedDecl::printNestedNameSpecifier,
1231
  // which can also print names for function and method scopes.
1232
1.29M
  if (DC->isFunctionOrMethod())
1233
14.7k
    return;
1234
1235
1.28M
  if (Policy.Callbacks && 
Policy.Callbacks->isScopeVisible(DC)368k
)
1236
0
    return;
1237
1238
1.28M
  if (const auto *NS = dyn_cast<NamespaceDecl>(DC)) {
1239
1.23M
    if (Policy.SuppressUnwrittenScope && 
NS->isAnonymousNamespace()10.1k
)
1240
206
      return AppendScope(DC->getParent(), OS, NameInScope);
1241
1242
    // Only suppress an inline namespace if the name has the same lookup
1243
    // results in the enclosing namespace.
1244
1.23M
    if (Policy.SuppressInlineNamespace && 
NS->isInline()284k
&&
NameInScope110k
&&
1245
1.23M
        
NS->isRedundantInlineQualifierFor(NameInScope)110k
)
1246
110k
      return AppendScope(DC->getParent(), OS, NameInScope);
1247
1248
1.12M
    AppendScope(DC->getParent(), OS, NS->getDeclName());
1249
1.12M
    if (NS->getIdentifier())
1250
1.11M
      OS << NS->getName() << "::";
1251
901
    else
1252
901
      OS << "(anonymous namespace)::";
1253
1.12M
  } else 
if (const auto *49.9k
Spec49.9k
= dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
1254
23.6k
    AppendScope(DC->getParent(), OS, Spec->getDeclName());
1255
23.6k
    IncludeStrongLifetimeRAII Strong(Policy);
1256
23.6k
    OS << Spec->getIdentifier()->getName();
1257
23.6k
    const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1258
23.6k
    printTemplateArgumentList(
1259
23.6k
        OS, TemplateArgs.asArray(), Policy,
1260
23.6k
        Spec->getSpecializedTemplate()->getTemplateParameters());
1261
23.6k
    OS << "::";
1262
26.2k
  } else if (const auto *Tag = dyn_cast<TagDecl>(DC)) {
1263
12.4k
    AppendScope(DC->getParent(), OS, Tag->getDeclName());
1264
12.4k
    if (TypedefNameDecl *Typedef = Tag->getTypedefNameForAnonDecl())
1265
18
      OS << Typedef->getIdentifier()->getName() << "::";
1266
12.4k
    else if (Tag->getIdentifier())
1267
12.2k
      OS << Tag->getIdentifier()->getName() << "::";
1268
164
    else
1269
164
      return;
1270
13.7k
  } else {
1271
13.7k
    AppendScope(DC->getParent(), OS, NameInScope);
1272
13.7k
  }
1273
1.28M
}
1274
1275
833k
void TypePrinter::printTag(TagDecl *D, raw_ostream &OS) {
1276
833k
  if (Policy.IncludeTagDefinition) {
1277
0
    PrintingPolicy SubPolicy = Policy;
1278
0
    SubPolicy.IncludeTagDefinition = false;
1279
0
    D->print(OS, SubPolicy, Indentation);
1280
0
    spaceBeforePlaceHolder(OS);
1281
0
    return;
1282
0
  }
1283
1284
833k
  bool HasKindDecoration = false;
1285
1286
  // We don't print tags unless this is an elaborated type.
1287
  // In C, we just assume every RecordType is an elaborated type.
1288
833k
  if (!Policy.SuppressTagKeyword && 
!D->getTypedefNameForAnonDecl()13.9k
) {
1289
13.6k
    HasKindDecoration = true;
1290
13.6k
    OS << D->getKindName();
1291
13.6k
    OS << ' ';
1292
13.6k
  }
1293
1294
  // Compute the full nested-name-specifier for this type.
1295
  // In C, this will always be empty except when the type
1296
  // being printed is anonymous within other Record.
1297
833k
  if (!Policy.SuppressScope)
1298
824k
    AppendScope(D->getDeclContext(), OS, D->getDeclName());
1299
1300
833k
  if (const IdentifierInfo *II = D->getIdentifier())
1301
816k
    OS << II->getName();
1302
17.5k
  else if (TypedefNameDecl *Typedef = D->getTypedefNameForAnonDecl()) {
1303
757
    assert(Typedef->getIdentifier() && "Typedef without identifier?");
1304
0
    OS << Typedef->getIdentifier()->getName();
1305
16.8k
  } else {
1306
    // Make an unambiguous representation for anonymous types, e.g.
1307
    //   (anonymous enum at /usr/include/string.h:120:9)
1308
16.8k
    OS << (Policy.MSVCFormatting ? 
'`'5
:
'('16.8k
);
1309
1310
16.8k
    if (isa<CXXRecordDecl>(D) && 
cast<CXXRecordDecl>(D)->isLambda()15.7k
) {
1311
8.81k
      OS << "lambda";
1312
8.81k
      HasKindDecoration = true;
1313
8.81k
    } else 
if (8.01k
(8.01k
isa<RecordDecl>(D)8.01k
&&
cast<RecordDecl>(D)->isAnonymousStructOrUnion()7.70k
)) {
1314
4.40k
      OS << "anonymous";
1315
4.40k
    } else {
1316
3.61k
      OS << "unnamed";
1317
3.61k
    }
1318
1319
16.8k
    if (Policy.AnonymousTagLocations) {
1320
      // Suppress the redundant tag keyword if we just printed one.
1321
      // We don't have to worry about ElaboratedTypes here because you can't
1322
      // refer to an anonymous type with one.
1323
16.8k
      if (!HasKindDecoration)
1324
7.17k
        OS << " " << D->getKindName();
1325
1326
16.8k
      PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc(
1327
16.8k
          D->getLocation());
1328
16.8k
      if (PLoc.isValid()) {
1329
10.3k
        OS << " at ";
1330
10.3k
        StringRef File = PLoc.getFilename();
1331
10.3k
        if (auto *Callbacks = Policy.Callbacks)
1332
5.33k
          OS << Callbacks->remapPath(File);
1333
4.97k
        else
1334
4.97k
          OS << File;
1335
10.3k
        OS << ':' << PLoc.getLine() << ':' << PLoc.getColumn();
1336
10.3k
      }
1337
16.8k
    }
1338
1339
16.8k
    OS << (Policy.MSVCFormatting ? 
'\''5
:
')'16.8k
);
1340
16.8k
  }
1341
1342
  // If this is a class template specialization, print the template
1343
  // arguments.
1344
833k
  if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
1345
482k
    ArrayRef<TemplateArgument> Args;
1346
482k
    TypeSourceInfo *TAW = Spec->getTypeAsWritten();
1347
482k
    if (!Policy.PrintCanonicalTypes && 
TAW295k
) {
1348
1.18k
      const TemplateSpecializationType *TST =
1349
1.18k
        cast<TemplateSpecializationType>(TAW->getType());
1350
1.18k
      Args = TST->template_arguments();
1351
481k
    } else {
1352
481k
      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1353
481k
      Args = TemplateArgs.asArray();
1354
481k
    }
1355
482k
    IncludeStrongLifetimeRAII Strong(Policy);
1356
482k
    printTemplateArgumentList(
1357
482k
        OS, Args, Policy,
1358
482k
        Spec->getSpecializedTemplate()->getTemplateParameters());
1359
482k
  }
1360
1361
833k
  spaceBeforePlaceHolder(OS);
1362
833k
}
1363
1364
840k
void TypePrinter::printRecordBefore(const RecordType *T, raw_ostream &OS) {
1365
  // Print the preferred name if we have one for this type.
1366
840k
  for (const auto *PNA : T->getDecl()->specific_attrs<PreferredNameAttr>()) {
1367
16.1k
    if (declaresSameEntity(PNA->getTypedefType()->getAsCXXRecordDecl(),
1368
16.1k
                           T->getDecl())) {
1369
      // Find the outermost typedef or alias template.
1370
16.1k
      QualType T = PNA->getTypedefType();
1371
16.1k
      while (true) {
1372
16.1k
        if (auto *TT = dyn_cast<TypedefType>(T))
1373
16.0k
          return printTypeSpec(TT->getDecl(), OS);
1374
104
        if (auto *TST = dyn_cast<TemplateSpecializationType>(T))
1375
52
          return printTemplateId(TST, OS, /*FullyQualify=*/true);
1376
52
        T = T->getLocallyUnqualifiedSingleStepDesugaredType();
1377
52
      }
1378
16.1k
    }
1379
16.1k
  }
1380
1381
824k
  printTag(T->getDecl(), OS);
1382
824k
}
1383
1384
840k
void TypePrinter::printRecordAfter(const RecordType *T, raw_ostream &OS) {}
1385
1386
9.92k
void TypePrinter::printEnumBefore(const EnumType *T, raw_ostream &OS) {
1387
9.92k
  printTag(T->getDecl(), OS);
1388
9.92k
}
1389
1390
9.92k
void TypePrinter::printEnumAfter(const EnumType *T, raw_ostream &OS) {}
1391
1392
void TypePrinter::printTemplateTypeParmBefore(const TemplateTypeParmType *T,
1393
183k
                                              raw_ostream &OS) {
1394
183k
  TemplateTypeParmDecl *D = T->getDecl();
1395
183k
  if (D && 
D->isImplicit()178k
) {
1396
150
    if (auto *TC = D->getTypeConstraint()) {
1397
17
      TC->print(OS, Policy);
1398
17
      OS << ' ';
1399
17
    }
1400
150
    OS << "auto";
1401
183k
  } else if (IdentifierInfo *Id = T->getIdentifier())
1402
178k
    OS << Id->getName();
1403
4.41k
  else
1404
4.41k
    OS << "type-parameter-" << T->getDepth() << '-' << T->getIndex();
1405
1406
183k
  spaceBeforePlaceHolder(OS);
1407
183k
}
1408
1409
void TypePrinter::printTemplateTypeParmAfter(const TemplateTypeParmType *T,
1410
184k
                                             raw_ostream &OS) {}
1411
1412
void TypePrinter::printSubstTemplateTypeParmBefore(
1413
                                             const SubstTemplateTypeParmType *T,
1414
17.5k
                                             raw_ostream &OS) {
1415
17.5k
  IncludeStrongLifetimeRAII Strong(Policy);
1416
17.5k
  printBefore(T->getReplacementType(), OS);
1417
17.5k
}
1418
1419
void TypePrinter::printSubstTemplateTypeParmAfter(
1420
                                             const SubstTemplateTypeParmType *T,
1421
19.2k
                                             raw_ostream &OS) {
1422
19.2k
  IncludeStrongLifetimeRAII Strong(Policy);
1423
19.2k
  printAfter(T->getReplacementType(), OS);
1424
19.2k
}
1425
1426
void TypePrinter::printSubstTemplateTypeParmPackBefore(
1427
                                        const SubstTemplateTypeParmPackType *T,
1428
14
                                        raw_ostream &OS) {
1429
14
  IncludeStrongLifetimeRAII Strong(Policy);
1430
14
  printTemplateTypeParmBefore(T->getReplacedParameter(), OS);
1431
14
}
1432
1433
void TypePrinter::printSubstTemplateTypeParmPackAfter(
1434
                                        const SubstTemplateTypeParmPackType *T,
1435
14
                                        raw_ostream &OS) {
1436
14
  IncludeStrongLifetimeRAII Strong(Policy);
1437
14
  printTemplateTypeParmAfter(T->getReplacedParameter(), OS);
1438
14
}
1439
1440
void TypePrinter::printTemplateId(const TemplateSpecializationType *T,
1441
123k
                                  raw_ostream &OS, bool FullyQualify) {
1442
123k
  IncludeStrongLifetimeRAII Strong(Policy);
1443
1444
123k
  TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl();
1445
123k
  if (FullyQualify && 
TD53
) {
1446
53
    if (!Policy.SuppressScope)
1447
19
      AppendScope(TD->getDeclContext(), OS, TD->getDeclName());
1448
1449
53
    IdentifierInfo *II = TD->getIdentifier();
1450
53
    OS << II->getName();
1451
123k
  } else {
1452
123k
    T->getTemplateName().print(OS, Policy);
1453
123k
  }
1454
1455
123k
  printTemplateArgumentList(OS, T->template_arguments(), Policy);
1456
123k
  spaceBeforePlaceHolder(OS);
1457
123k
}
1458
1459
void TypePrinter::printTemplateSpecializationBefore(
1460
                                            const TemplateSpecializationType *T,
1461
123k
                                            raw_ostream &OS) {
1462
123k
  printTemplateId(T, OS, Policy.FullyQualifiedName);
1463
123k
}
1464
1465
void TypePrinter::printTemplateSpecializationAfter(
1466
                                            const TemplateSpecializationType *T,
1467
6.34k
                                            raw_ostream &OS) {}
1468
1469
void TypePrinter::printInjectedClassNameBefore(const InjectedClassNameType *T,
1470
117k
                                               raw_ostream &OS) {
1471
117k
  if (Policy.PrintInjectedClassNameWithArguments)
1472
117k
    return printTemplateSpecializationBefore(T->getInjectedTST(), OS);
1473
1474
0
  IncludeStrongLifetimeRAII Strong(Policy);
1475
0
  T->getTemplateName().print(OS, Policy);
1476
0
  spaceBeforePlaceHolder(OS);
1477
0
}
1478
1479
void TypePrinter::printInjectedClassNameAfter(const InjectedClassNameType *T,
1480
117k
                                               raw_ostream &OS) {}
1481
1482
void TypePrinter::printElaboratedBefore(const ElaboratedType *T,
1483
4.74k
                                        raw_ostream &OS) {
1484
4.74k
  if (Policy.IncludeTagDefinition && 
T->getOwnedTagDecl()246
) {
1485
246
    TagDecl *OwnedTagDecl = T->getOwnedTagDecl();
1486
246
    assert(OwnedTagDecl->getTypeForDecl() == T->getNamedType().getTypePtr() &&
1487
246
           "OwnedTagDecl expected to be a declaration for the type");
1488
0
    PrintingPolicy SubPolicy = Policy;
1489
246
    SubPolicy.IncludeTagDefinition = false;
1490
246
    OwnedTagDecl->print(OS, SubPolicy, Indentation);
1491
246
    spaceBeforePlaceHolder(OS);
1492
246
    return;
1493
246
  }
1494
1495
  // The tag definition will take care of these.
1496
4.49k
  if (!Policy.IncludeTagDefinition)
1497
4.49k
  {
1498
4.49k
    OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1499
4.49k
    if (T->getKeyword() != ETK_None)
1500
3.38k
      OS << " ";
1501
4.49k
    NestedNameSpecifier *Qualifier = T->getQualifier();
1502
4.49k
    if (Qualifier)
1503
1.67k
      Qualifier->print(OS, Policy);
1504
4.49k
  }
1505
1506
4.49k
  ElaboratedTypePolicyRAII PolicyRAII(Policy);
1507
4.49k
  printBefore(T->getNamedType(), OS);
1508
4.49k
}
1509
1510
void TypePrinter::printElaboratedAfter(const ElaboratedType *T,
1511
4.76k
                                        raw_ostream &OS) {
1512
4.76k
  if (Policy.IncludeTagDefinition && 
T->getOwnedTagDecl()246
)
1513
246
    return;
1514
4.52k
  ElaboratedTypePolicyRAII PolicyRAII(Policy);
1515
4.52k
  printAfter(T->getNamedType(), OS);
1516
4.52k
}
1517
1518
2.65k
void TypePrinter::printParenBefore(const ParenType *T, raw_ostream &OS) {
1519
2.65k
  if (!HasEmptyPlaceHolder && 
!isa<FunctionType>(T->getInnerType())2.43k
) {
1520
221
    printBefore(T->getInnerType(), OS);
1521
221
    OS << '(';
1522
221
  } else
1523
2.43k
    printBefore(T->getInnerType(), OS);
1524
2.65k
}
1525
1526
2.65k
void TypePrinter::printParenAfter(const ParenType *T, raw_ostream &OS) {
1527
2.65k
  if (!HasEmptyPlaceHolder && 
!isa<FunctionType>(T->getInnerType())2.43k
) {
1528
220
    OS << ')';
1529
220
    printAfter(T->getInnerType(), OS);
1530
220
  } else
1531
2.43k
    printAfter(T->getInnerType(), OS);
1532
2.65k
}
1533
1534
void TypePrinter::printDependentNameBefore(const DependentNameType *T,
1535
237
                                           raw_ostream &OS) {
1536
237
  OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1537
237
  if (T->getKeyword() != ETK_None)
1538
230
    OS << " ";
1539
1540
237
  T->getQualifier()->print(OS, Policy);
1541
1542
237
  OS << T->getIdentifier()->getName();
1543
237
  spaceBeforePlaceHolder(OS);
1544
237
}
1545
1546
void TypePrinter::printDependentNameAfter(const DependentNameType *T,
1547
237
                                          raw_ostream &OS) {}
1548
1549
void TypePrinter::printDependentTemplateSpecializationBefore(
1550
13
        const DependentTemplateSpecializationType *T, raw_ostream &OS) {
1551
13
  IncludeStrongLifetimeRAII Strong(Policy);
1552
1553
13
  OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1554
13
  if (T->getKeyword() != ETK_None)
1555
13
    OS << " ";
1556
1557
13
  if (T->getQualifier())
1558
13
    T->getQualifier()->print(OS, Policy);
1559
13
  OS << "template " << T->getIdentifier()->getName();
1560
13
  printTemplateArgumentList(OS, T->template_arguments(), Policy);
1561
13
  spaceBeforePlaceHolder(OS);
1562
13
}
1563
1564
void TypePrinter::printDependentTemplateSpecializationAfter(
1565
13
        const DependentTemplateSpecializationType *T, raw_ostream &OS) {}
1566
1567
void TypePrinter::printPackExpansionBefore(const PackExpansionType *T,
1568
37.1k
                                           raw_ostream &OS) {
1569
37.1k
  printBefore(T->getPattern(), OS);
1570
37.1k
}
1571
1572
void TypePrinter::printPackExpansionAfter(const PackExpansionType *T,
1573
37.1k
                                          raw_ostream &OS) {
1574
37.1k
  printAfter(T->getPattern(), OS);
1575
37.1k
  OS << "...";
1576
37.1k
}
1577
1578
void TypePrinter::printAttributedBefore(const AttributedType *T,
1579
1.48k
                                        raw_ostream &OS) {
1580
  // FIXME: Generate this with TableGen.
1581
1582
  // Prefer the macro forms of the GC and ownership qualifiers.
1583
1.48k
  if (T->getAttrKind() == attr::ObjCGC ||
1584
1.48k
      
T->getAttrKind() == attr::ObjCOwnership1.44k
)
1585
493
    return printBefore(T->getEquivalentType(), OS);
1586
1587
994
  if (T->getAttrKind() == attr::ObjCKindOf)
1588
76
    OS << "__kindof ";
1589
1590
994
  if (T->getAttrKind() == attr::AddressSpace)
1591
65
    printBefore(T->getEquivalentType(), OS);
1592
929
  else
1593
929
    printBefore(T->getModifiedType(), OS);
1594
1595
994
  if (T->isMSTypeSpec()) {
1596
11
    switch (T->getAttrKind()) {
1597
0
    default: return;
1598
5
    case attr::Ptr32: OS << " __ptr32"; break;
1599
2
    case attr::Ptr64: OS << " __ptr64"; break;
1600
2
    case attr::SPtr: OS << " __sptr"; break;
1601
2
    case attr::UPtr: OS << " __uptr"; break;
1602
11
    }
1603
11
    spaceBeforePlaceHolder(OS);
1604
11
  }
1605
1606
  // Print nullability type specifiers.
1607
994
  if (T->getImmediateNullability()) {
1608
575
    if (T->getAttrKind() == attr::TypeNonNull)
1609
196
      OS << " _Nonnull";
1610
379
    else if (T->getAttrKind() == attr::TypeNullable)
1611
355
      OS << " _Nullable";
1612
24
    else if (T->getAttrKind() == attr::TypeNullUnspecified)
1613
16
      OS << " _Null_unspecified";
1614
8
    else if (T->getAttrKind() == attr::TypeNullableResult)
1615
8
      OS << " _Nullable_result";
1616
0
    else
1617
0
      llvm_unreachable("unhandled nullability");
1618
575
    spaceBeforePlaceHolder(OS);
1619
575
  }
1620
994
}
1621
1622
void TypePrinter::printAttributedAfter(const AttributedType *T,
1623
1.42k
                                       raw_ostream &OS) {
1624
  // FIXME: Generate this with TableGen.
1625
1626
  // Prefer the macro forms of the GC and ownership qualifiers.
1627
1.42k
  if (T->getAttrKind() == attr::ObjCGC ||
1628
1.42k
      
T->getAttrKind() == attr::ObjCOwnership1.38k
)
1629
493
    return printAfter(T->getEquivalentType(), OS);
1630
1631
  // If this is a calling convention attribute, don't print the implicit CC from
1632
  // the modified type.
1633
935
  SaveAndRestore<bool> MaybeSuppressCC(InsideCCAttribute, T->isCallingConv());
1634
1635
935
  printAfter(T->getModifiedType(), OS);
1636
1637
  // Some attributes are printed as qualifiers before the type, so we have
1638
  // nothing left to do.
1639
935
  if (T->getAttrKind() == attr::ObjCKindOf ||
1640
935
      
T->isMSTypeSpec()918
||
T->getImmediateNullability()907
)
1641
603
    return;
1642
1643
  // Don't print the inert __unsafe_unretained attribute at all.
1644
332
  if (T->getAttrKind() == attr::ObjCInertUnsafeUnretained)
1645
2
    return;
1646
1647
  // Don't print ns_returns_retained unless it had an effect.
1648
330
  if (T->getAttrKind() == attr::NSReturnsRetained &&
1649
330
      !T->getEquivalentType()->castAs<FunctionType>()
1650
3
                             ->getExtInfo().getProducesResult())
1651
0
    return;
1652
1653
330
  if (T->getAttrKind() == attr::LifetimeBound) {
1654
0
    OS << " [[clang::lifetimebound]]";
1655
0
    return;
1656
0
  }
1657
1658
  // The printing of the address_space attribute is handled by the qualifier
1659
  // since it is still stored in the qualifier. Return early to prevent printing
1660
  // this twice.
1661
330
  if (T->getAttrKind() == attr::AddressSpace)
1662
65
    return;
1663
1664
265
  OS << " __attribute__((";
1665
265
  switch (T->getAttrKind()) {
1666
0
#define TYPE_ATTR(NAME)
1667
0
#define DECL_OR_TYPE_ATTR(NAME)
1668
0
#define ATTR(NAME) case attr::NAME:
1669
0
#include "clang/Basic/AttrList.inc"
1670
0
    llvm_unreachable("non-type attribute attached to type");
1671
1672
0
  case attr::OpenCLPrivateAddressSpace:
1673
0
  case attr::OpenCLGlobalAddressSpace:
1674
0
  case attr::OpenCLGlobalDeviceAddressSpace:
1675
0
  case attr::OpenCLGlobalHostAddressSpace:
1676
0
  case attr::OpenCLLocalAddressSpace:
1677
0
  case attr::OpenCLConstantAddressSpace:
1678
0
  case attr::OpenCLGenericAddressSpace:
1679
    // FIXME: Update printAttributedBefore to print these once we generate
1680
    // AttributedType nodes for them.
1681
0
    break;
1682
1683
0
  case attr::LifetimeBound:
1684
0
  case attr::TypeNonNull:
1685
0
  case attr::TypeNullable:
1686
0
  case attr::TypeNullableResult:
1687
0
  case attr::TypeNullUnspecified:
1688
0
  case attr::ObjCGC:
1689
0
  case attr::ObjCInertUnsafeUnretained:
1690
0
  case attr::ObjCKindOf:
1691
0
  case attr::ObjCOwnership:
1692
0
  case attr::Ptr32:
1693
0
  case attr::Ptr64:
1694
0
  case attr::SPtr:
1695
0
  case attr::UPtr:
1696
0
  case attr::AddressSpace:
1697
0
  case attr::CmseNSCall:
1698
0
    llvm_unreachable("This attribute should have been handled already");
1699
1700
3
  case attr::NSReturnsRetained:
1701
3
    OS << "ns_returns_retained";
1702
3
    break;
1703
1704
  // FIXME: When Sema learns to form this AttributedType, avoid printing the
1705
  // attribute again in printFunctionProtoAfter.
1706
0
  case attr::AnyX86NoCfCheck: OS << "nocf_check"; break;
1707
99
  case attr::CDecl: OS << "cdecl"; break;
1708
25
  case attr::FastCall: OS << "fastcall"; break;
1709
26
  case attr::StdCall: OS << "stdcall"; break;
1710
29
  case attr::ThisCall: OS << "thiscall"; break;
1711
0
  case attr::SwiftCall: OS << "swiftcall"; break;
1712
0
  case attr::SwiftAsyncCall: OS << "swiftasynccall"; break;
1713
23
  case attr::VectorCall: OS << "vectorcall"; break;
1714
0
  case attr::Pascal: OS << "pascal"; break;
1715
6
  case attr::MSABI: OS << "ms_abi"; break;
1716
6
  case attr::SysVABI: OS << "sysv_abi"; break;
1717
2
  case attr::RegCall: OS << "regcall"; break;
1718
0
  case attr::Pcs: {
1719
0
    OS << "pcs(";
1720
0
   QualType t = T->getEquivalentType();
1721
0
   while (!t->isFunctionType())
1722
0
     t = t->getPointeeType();
1723
0
   OS << (t->castAs<FunctionType>()->getCallConv() == CC_AAPCS ?
1724
0
         "\"aapcs\"" : "\"aapcs-vfp\"");
1725
0
   OS << ')';
1726
0
   break;
1727
0
  }
1728
0
  case attr::AArch64VectorPcs: OS << "aarch64_vector_pcs"; break;
1729
0
  case attr::IntelOclBicc: OS << "inteloclbicc"; break;
1730
20
  case attr::PreserveMost:
1731
20
    OS << "preserve_most";
1732
20
    break;
1733
1734
20
  case attr::PreserveAll:
1735
20
    OS << "preserve_all";
1736
20
    break;
1737
6
  case attr::NoDeref:
1738
6
    OS << "noderef";
1739
6
    break;
1740
0
  case attr::AcquireHandle:
1741
0
    OS << "acquire_handle";
1742
0
    break;
1743
0
  case attr::ArmMveStrictPolymorphism:
1744
0
    OS << "__clang_arm_mve_strict_polymorphism";
1745
0
    break;
1746
265
  }
1747
265
  OS << "))";
1748
265
}
1749
1750
void TypePrinter::printObjCInterfaceBefore(const ObjCInterfaceType *T,
1751
35.4k
                                           raw_ostream &OS) {
1752
35.4k
  OS << T->getDecl()->getName();
1753
35.4k
  spaceBeforePlaceHolder(OS);
1754
35.4k
}
1755
1756
void TypePrinter::printObjCInterfaceAfter(const ObjCInterfaceType *T,
1757
14.3k
                                          raw_ostream &OS) {}
1758
1759
void TypePrinter::printObjCTypeParamBefore(const ObjCTypeParamType *T,
1760
15
                                          raw_ostream &OS) {
1761
15
  OS << T->getDecl()->getName();
1762
15
  if (!T->qual_empty()) {
1763
0
    bool isFirst = true;
1764
0
    OS << '<';
1765
0
    for (const auto *I : T->quals()) {
1766
0
      if (isFirst)
1767
0
        isFirst = false;
1768
0
      else
1769
0
        OS << ',';
1770
0
      OS << I->getName();
1771
0
    }
1772
0
    OS << '>';
1773
0
  }
1774
1775
15
  spaceBeforePlaceHolder(OS);
1776
15
}
1777
1778
void TypePrinter::printObjCTypeParamAfter(const ObjCTypeParamType *T,
1779
15
                                          raw_ostream &OS) {}
1780
1781
void TypePrinter::printObjCObjectBefore(const ObjCObjectType *T,
1782
7.05k
                                        raw_ostream &OS) {
1783
7.05k
  if (T->qual_empty() && 
T->isUnspecializedAsWritten()5.57k
&&
1784
7.05k
      
!T->isKindOfTypeAsWritten()4.89k
)
1785
4.83k
    return printBefore(T->getBaseType(), OS);
1786
1787
2.22k
  if (T->isKindOfTypeAsWritten())
1788
68
    OS << "__kindof ";
1789
1790
2.22k
  print(T->getBaseType(), OS, StringRef());
1791
1792
2.22k
  if (T->isSpecializedAsWritten()) {
1793
687
    bool isFirst = true;
1794
687
    OS << '<';
1795
716
    for (auto typeArg : T->getTypeArgsAsWritten()) {
1796
716
      if (isFirst)
1797
687
        isFirst = false;
1798
29
      else
1799
29
        OS << ",";
1800
1801
716
      print(typeArg, OS, StringRef());
1802
716
    }
1803
687
    OS << '>';
1804
687
  }
1805
1806
2.22k
  if (!T->qual_empty()) {
1807
1.48k
    bool isFirst = true;
1808
1.48k
    OS << '<';
1809
2.34k
    for (const auto *I : T->quals()) {
1810
2.34k
      if (isFirst)
1811
1.48k
        isFirst = false;
1812
859
      else
1813
859
        OS << ',';
1814
2.34k
      OS << I->getName();
1815
2.34k
    }
1816
1.48k
    OS << '>';
1817
1.48k
  }
1818
1819
2.22k
  spaceBeforePlaceHolder(OS);
1820
2.22k
}
1821
1822
void TypePrinter::printObjCObjectAfter(const ObjCObjectType *T,
1823
2.03k
                                        raw_ostream &OS) {
1824
2.03k
  if (T->qual_empty() && 
T->isUnspecializedAsWritten()2.01k
&&
1825
2.03k
      
!T->isKindOfTypeAsWritten()2.00k
)
1826
2.00k
    return printAfter(T->getBaseType(), OS);
1827
2.03k
}
1828
1829
void TypePrinter::printObjCObjectPointerBefore(const ObjCObjectPointerType *T,
1830
26.1k
                                               raw_ostream &OS) {
1831
26.1k
  printBefore(T->getPointeeType(), OS);
1832
1833
  // If we need to print the pointer, print it now.
1834
26.1k
  if (!T->isObjCIdType() && 
!T->isObjCQualifiedIdType()23.7k
&&
1835
26.1k
      
!T->isObjCClassType()22.5k
&&
!T->isObjCQualifiedClassType()22.1k
) {
1836
22.0k
    if (HasEmptyPlaceHolder)
1837
20.8k
      OS << ' ';
1838
22.0k
    OS << '*';
1839
22.0k
  }
1840
26.1k
}
1841
1842
void TypePrinter::printObjCObjectPointerAfter(const ObjCObjectPointerType *T,
1843
26.1k
                                              raw_ostream &OS) {}
1844
1845
static
1846
1.64M
const TemplateArgument &getArgument(const TemplateArgument &A) { return A; }
1847
1848
1.78k
static const TemplateArgument &getArgument(const TemplateArgumentLoc &A) {
1849
1.78k
  return A.getArgument();
1850
1.78k
}
1851
1852
static void printArgument(const TemplateArgument &A, const PrintingPolicy &PP,
1853
1.50M
                          llvm::raw_ostream &OS, bool IncludeType) {
1854
1.50M
  A.print(PP, OS, IncludeType);
1855
1.50M
}
1856
1857
static void printArgument(const TemplateArgumentLoc &A,
1858
                          const PrintingPolicy &PP, llvm::raw_ostream &OS,
1859
1.47k
                          bool IncludeType) {
1860
1.47k
  const TemplateArgument::ArgKind &Kind = A.getArgument().getKind();
1861
1.47k
  if (Kind == TemplateArgument::ArgKind::Type)
1862
1.06k
    return A.getTypeSourceInfo()->getType().print(OS, PP);
1863
402
  return A.getArgument().print(PP, OS, IncludeType);
1864
1.47k
}
1865
1866
static bool isSubstitutedTemplateArgument(ASTContext &Ctx, TemplateArgument Arg,
1867
                                          TemplateArgument Pattern,
1868
                                          ArrayRef<TemplateArgument> Args,
1869
                                          unsigned Depth);
1870
1871
static bool isSubstitutedType(ASTContext &Ctx, QualType T, QualType Pattern,
1872
6.21k
                              ArrayRef<TemplateArgument> Args, unsigned Depth) {
1873
6.21k
  if (Ctx.hasSameType(T, Pattern))
1874
0
    return true;
1875
1876
  // A type parameter matches its argument.
1877
6.21k
  if (auto *TTPT = Pattern->getAs<TemplateTypeParmType>()) {
1878
2.84k
    if (TTPT->getDepth() == Depth && TTPT->getIndex() < Args.size() &&
1879
2.84k
        Args[TTPT->getIndex()].getKind() == TemplateArgument::Type) {
1880
2.84k
      QualType SubstArg = Ctx.getQualifiedType(
1881
2.84k
          Args[TTPT->getIndex()].getAsType(), Pattern.getQualifiers());
1882
2.84k
      return Ctx.hasSameType(SubstArg, T);
1883
2.84k
    }
1884
0
    return false;
1885
2.84k
  }
1886
1887
  // FIXME: Recurse into array types.
1888
1889
  // All other cases will need the types to be identically qualified.
1890
3.36k
  Qualifiers TQual, PatQual;
1891
3.36k
  T = Ctx.getUnqualifiedArrayType(T, TQual);
1892
3.36k
  Pattern = Ctx.getUnqualifiedArrayType(Pattern, PatQual);
1893
3.36k
  if (TQual != PatQual)
1894
40
    return false;
1895
1896
  // Recurse into pointer-like types.
1897
3.32k
  {
1898
3.32k
    QualType TPointee = T->getPointeeType();
1899
3.32k
    QualType PPointee = Pattern->getPointeeType();
1900
3.32k
    if (!TPointee.isNull() && 
!PPointee.isNull()71
)
1901
0
      return T->getTypeClass() == Pattern->getTypeClass() &&
1902
0
             isSubstitutedType(Ctx, TPointee, PPointee, Args, Depth);
1903
3.32k
  }
1904
1905
  // Recurse into template specialization types.
1906
3.32k
  if (auto *PTST =
1907
3.32k
          Pattern.getCanonicalType()->getAs<TemplateSpecializationType>()) {
1908
3.07k
    TemplateName Template;
1909
3.07k
    ArrayRef<TemplateArgument> TemplateArgs;
1910
3.07k
    if (auto *TTST = T->getAs<TemplateSpecializationType>()) {
1911
0
      Template = TTST->getTemplateName();
1912
0
      TemplateArgs = TTST->template_arguments();
1913
3.07k
    } else if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(
1914
3.07k
                   T->getAsCXXRecordDecl())) {
1915
2.94k
      Template = TemplateName(CTSD->getSpecializedTemplate());
1916
2.94k
      TemplateArgs = CTSD->getTemplateArgs().asArray();
1917
2.94k
    } else {
1918
128
      return false;
1919
128
    }
1920
1921
2.94k
    if (!isSubstitutedTemplateArgument(Ctx, Template, PTST->getTemplateName(),
1922
2.94k
                                       Args, Depth))
1923
81
      return false;
1924
2.86k
    if (TemplateArgs.size() != PTST->getNumArgs())
1925
0
      return false;
1926
5.73k
    
for (unsigned I = 0, N = TemplateArgs.size(); 2.86k
I != N;
++I2.86k
)
1927
2.90k
      if (!isSubstitutedTemplateArgument(Ctx, TemplateArgs[I], PTST->getArg(I),
1928
2.90k
                                         Args, Depth))
1929
40
        return false;
1930
2.82k
    return true;
1931
2.86k
  }
1932
1933
  // FIXME: Handle more cases.
1934
254
  return false;
1935
3.32k
}
1936
1937
static bool isSubstitutedTemplateArgument(ASTContext &Ctx, TemplateArgument Arg,
1938
                                          TemplateArgument Pattern,
1939
                                          ArrayRef<TemplateArgument> Args,
1940
10.4k
                                          unsigned Depth) {
1941
10.4k
  Arg = Ctx.getCanonicalTemplateArgument(Arg);
1942
10.4k
  Pattern = Ctx.getCanonicalTemplateArgument(Pattern);
1943
10.4k
  if (Arg.structurallyEquals(Pattern))
1944
3.37k
    return true;
1945
1946
7.05k
  if (Pattern.getKind() == TemplateArgument::Expression) {
1947
665
    if (auto *DRE =
1948
665
            dyn_cast<DeclRefExpr>(Pattern.getAsExpr()->IgnoreParenImpCasts())) {
1949
101
      if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()))
1950
1
        return NTTP->getDepth() == Depth && Args.size() > NTTP->getIndex() &&
1951
1
               Args[NTTP->getIndex()].structurallyEquals(Arg);
1952
101
    }
1953
665
  }
1954
1955
7.05k
  if (Arg.getKind() != Pattern.getKind())
1956
664
    return false;
1957
1958
6.38k
  if (Arg.getKind() == TemplateArgument::Type)
1959
6.21k
    return isSubstitutedType(Ctx, Arg.getAsType(), Pattern.getAsType(), Args,
1960
6.21k
                             Depth);
1961
1962
177
  if (Arg.getKind() == TemplateArgument::Template) {
1963
177
    TemplateDecl *PatTD = Pattern.getAsTemplate().getAsTemplateDecl();
1964
177
    if (auto *TTPD = dyn_cast_or_null<TemplateTemplateParmDecl>(PatTD))
1965
12
      return TTPD->getDepth() == Depth && Args.size() > TTPD->getIndex() &&
1966
12
             Ctx.getCanonicalTemplateArgument(Args[TTPD->getIndex()])
1967
12
                 .structurallyEquals(Arg);
1968
177
  }
1969
1970
  // FIXME: Handle more cases.
1971
165
  return false;
1972
177
}
1973
1974
/// Make a best-effort determination of whether the type T can be produced by
1975
/// substituting Args into the default argument of Param.
1976
static bool isSubstitutedDefaultArgument(ASTContext &Ctx, TemplateArgument Arg,
1977
                                         const NamedDecl *Param,
1978
                                         ArrayRef<TemplateArgument> Args,
1979
30.8k
                                         unsigned Depth) {
1980
  // An empty pack is equivalent to not providing a pack argument.
1981
30.8k
  if (Arg.getKind() == TemplateArgument::Pack && 
Arg.pack_size() == 0826
)
1982
192
    return true;
1983
1984
30.6k
  if (auto *TTPD = dyn_cast<TemplateTypeParmDecl>(Param)) {
1985
21.6k
    return TTPD->hasDefaultArgument() &&
1986
21.6k
           isSubstitutedTemplateArgument(Ctx, Arg, TTPD->getDefaultArgument(),
1987
3.69k
                                         Args, Depth);
1988
21.6k
  } else 
if (auto *8.92k
TTPD8.92k
= dyn_cast<TemplateTemplateParmDecl>(Param)) {
1989
550
    return TTPD->hasDefaultArgument() &&
1990
550
           isSubstitutedTemplateArgument(
1991
263
               Ctx, Arg, TTPD->getDefaultArgument().getArgument(), Args, Depth);
1992
8.37k
  } else if (auto *NTTPD = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1993
8.37k
    return NTTPD->hasDefaultArgument() &&
1994
8.37k
           isSubstitutedTemplateArgument(Ctx, Arg, NTTPD->getDefaultArgument(),
1995
625
                                         Args, Depth);
1996
8.37k
  }
1997
0
  return false;
1998
30.6k
}
1999
2000
template <typename TA>
2001
static void printTo(raw_ostream &OS, ArrayRef<TA> Args,
2002
                    const PrintingPolicy &Policy, bool SkipBrackets,
2003
                    const TemplateParameterList *TPL, bool IsPack,
2004
938k
                    unsigned ParmIndex) {
2005
  // Drop trailing template arguments that match default arguments.
2006
938k
  if (TPL && 
Policy.SuppressDefaultTemplateArgs714k
&&
2007
938k
      
!Policy.PrintCanonicalTypes354k
&&
!Args.empty()28.3k
&&
!IsPack28.3k
&&
2008
938k
      
Args.size() <= TPL->size()27.6k
) {
2009
27.6k
    ASTContext &Ctx = TPL->getParam(0)->getASTContext();
2010
27.6k
    llvm::SmallVector<TemplateArgument, 8> OrigArgs;
2011
27.6k
    for (const TA &A : Args)
2012
38.5k
      OrigArgs.push_back(getArgument(A));
2013
31.1k
    while (!Args.empty() &&
2014
31.1k
           isSubstitutedDefaultArgument(Ctx, getArgument(Args.back()),
2015
30.8k
                                        TPL->getParam(Args.size() - 1),
2016
30.8k
                                        OrigArgs, TPL->getDepth()))
2017
3.51k
      Args = Args.drop_back();
2018
27.6k
  }
2019
2020
938k
  const char *Comma = Policy.MSVCFormatting ? 
","55
:
", "938k
;
2021
938k
  if (!SkipBrackets)
2022
868k
    OS << '<';
2023
2024
938k
  bool NeedSpace = false;
2025
938k
  bool FirstArg = true;
2026
1.57M
  for (const auto &Arg : Args) {
2027
    // Print the argument into a string.
2028
1.57M
    SmallString<128> Buf;
2029
1.57M
    llvm::raw_svector_ostream ArgOS(Buf);
2030
1.57M
    const TemplateArgument &Argument = getArgument(Arg);
2031
1.57M
    if (Argument.getKind() == TemplateArgument::Pack) {
2032
70.4k
      if (Argument.pack_size() && 
!FirstArg65.3k
)
2033
10.3k
        OS << Comma;
2034
70.4k
      printTo(ArgOS, Argument.getPackAsArray(), Policy, true, TPL,
2035
70.4k
              /*IsPack*/ true, ParmIndex);
2036
1.50M
    } else {
2037
1.50M
      if (!FirstArg)
2038
627k
        OS << Comma;
2039
      // Tries to print the argument with location info if exists.
2040
1.50M
      printArgument(
2041
1.50M
          Arg, Policy, ArgOS,
2042
1.50M
          TemplateParameterList::shouldIncludeTypeForArgument(TPL, ParmIndex));
2043
1.50M
    }
2044
1.57M
    StringRef ArgString = ArgOS.str();
2045
2046
    // If this is the first argument and its string representation
2047
    // begins with the global scope specifier ('::foo'), add a space
2048
    // to avoid printing the diagraph '<:'.
2049
1.57M
    if (FirstArg && 
!ArgString.empty()932k
&&
ArgString[0] == ':'931k
)
2050
3
      OS << ' ';
2051
2052
1.57M
    OS << ArgString;
2053
2054
    // If the last character of our string is '>', add another space to
2055
    // keep the two '>''s separate tokens.
2056
1.57M
    NeedSpace = Policy.SplitTemplateClosers && 
!ArgString.empty()1.27M
&&
2057
1.57M
                
ArgString.back() == '>'1.26M
;
2058
1.57M
    FirstArg = false;
2059
2060
    // Use same template parameter for all elements of Pack
2061
1.57M
    if (!IsPack)
2062
1.47M
      ParmIndex++;
2063
1.57M
  }
2064
2065
938k
  if (NeedSpace)
2066
196k
    OS << ' ';
2067
2068
938k
  if (!SkipBrackets)
2069
868k
    OS << '>';
2070
938k
}
TypePrinter.cpp:void printTo<clang::TemplateArgument>(llvm::raw_ostream&, llvm::ArrayRef<clang::TemplateArgument>, clang::PrintingPolicy const&, bool, clang::TemplateParameterList const*, bool, unsigned int)
Line
Count
Source
2004
937k
                    unsigned ParmIndex) {
2005
  // Drop trailing template arguments that match default arguments.
2006
937k
  if (TPL && 
Policy.SuppressDefaultTemplateArgs713k
&&
2007
937k
      
!Policy.PrintCanonicalTypes354k
&&
!Args.empty()28.1k
&&
!IsPack28.1k
&&
2008
937k
      
Args.size() <= TPL->size()27.5k
) {
2009
27.5k
    ASTContext &Ctx = TPL->getParam(0)->getASTContext();
2010
27.5k
    llvm::SmallVector<TemplateArgument, 8> OrigArgs;
2011
27.5k
    for (const TA &A : Args)
2012
38.3k
      OrigArgs.push_back(getArgument(A));
2013
31.0k
    while (!Args.empty() &&
2014
31.0k
           isSubstitutedDefaultArgument(Ctx, getArgument(Args.back()),
2015
30.6k
                                        TPL->getParam(Args.size() - 1),
2016
30.6k
                                        OrigArgs, TPL->getDepth()))
2017
3.51k
      Args = Args.drop_back();
2018
27.5k
  }
2019
2020
937k
  const char *Comma = Policy.MSVCFormatting ? 
","55
:
", "937k
;
2021
937k
  if (!SkipBrackets)
2022
867k
    OS << '<';
2023
2024
937k
  bool NeedSpace = false;
2025
937k
  bool FirstArg = true;
2026
1.57M
  for (const auto &Arg : Args) {
2027
    // Print the argument into a string.
2028
1.57M
    SmallString<128> Buf;
2029
1.57M
    llvm::raw_svector_ostream ArgOS(Buf);
2030
1.57M
    const TemplateArgument &Argument = getArgument(Arg);
2031
1.57M
    if (Argument.getKind() == TemplateArgument::Pack) {
2032
70.4k
      if (Argument.pack_size() && 
!FirstArg65.3k
)
2033
10.3k
        OS << Comma;
2034
70.4k
      printTo(ArgOS, Argument.getPackAsArray(), Policy, true, TPL,
2035
70.4k
              /*IsPack*/ true, ParmIndex);
2036
1.50M
    } else {
2037
1.50M
      if (!FirstArg)
2038
627k
        OS << Comma;
2039
      // Tries to print the argument with location info if exists.
2040
1.50M
      printArgument(
2041
1.50M
          Arg, Policy, ArgOS,
2042
1.50M
          TemplateParameterList::shouldIncludeTypeForArgument(TPL, ParmIndex));
2043
1.50M
    }
2044
1.57M
    StringRef ArgString = ArgOS.str();
2045
2046
    // If this is the first argument and its string representation
2047
    // begins with the global scope specifier ('::foo'), add a space
2048
    // to avoid printing the diagraph '<:'.
2049
1.57M
    if (FirstArg && 
!ArgString.empty()932k
&&
ArgString[0] == ':'930k
)
2050
3
      OS << ' ';
2051
2052
1.57M
    OS << ArgString;
2053
2054
    // If the last character of our string is '>', add another space to
2055
    // keep the two '>''s separate tokens.
2056
1.57M
    NeedSpace = Policy.SplitTemplateClosers && 
!ArgString.empty()1.27M
&&
2057
1.57M
                
ArgString.back() == '>'1.26M
;
2058
1.57M
    FirstArg = false;
2059
2060
    // Use same template parameter for all elements of Pack
2061
1.57M
    if (!IsPack)
2062
1.47M
      ParmIndex++;
2063
1.57M
  }
2064
2065
937k
  if (NeedSpace)
2066
196k
    OS << ' ';
2067
2068
937k
  if (!SkipBrackets)
2069
867k
    OS << '>';
2070
937k
}
TypePrinter.cpp:void printTo<clang::TemplateArgumentLoc>(llvm::raw_ostream&, llvm::ArrayRef<clang::TemplateArgumentLoc>, clang::PrintingPolicy const&, bool, clang::TemplateParameterList const*, bool, unsigned int)
Line
Count
Source
2004
923
                    unsigned ParmIndex) {
2005
  // Drop trailing template arguments that match default arguments.
2006
923
  if (TPL && 
Policy.SuppressDefaultTemplateArgs161
&&
2007
923
      
!Policy.PrintCanonicalTypes161
&&
!Args.empty()154
&&
!IsPack154
&&
2008
923
      
Args.size() <= TPL->size()154
) {
2009
137
    ASTContext &Ctx = TPL->getParam(0)->getASTContext();
2010
137
    llvm::SmallVector<TemplateArgument, 8> OrigArgs;
2011
137
    for (const TA &A : Args)
2012
171
      OrigArgs.push_back(getArgument(A));
2013
138
    while (!Args.empty() &&
2014
138
           isSubstitutedDefaultArgument(Ctx, getArgument(Args.back()),
2015
138
                                        TPL->getParam(Args.size() - 1),
2016
138
                                        OrigArgs, TPL->getDepth()))
2017
1
      Args = Args.drop_back();
2018
137
  }
2019
2020
923
  const char *Comma = Policy.MSVCFormatting ? 
","0
: ", ";
2021
923
  if (!SkipBrackets)
2022
923
    OS << '<';
2023
2024
923
  bool NeedSpace = false;
2025
923
  bool FirstArg = true;
2026
1.47k
  for (const auto &Arg : Args) {
2027
    // Print the argument into a string.
2028
1.47k
    SmallString<128> Buf;
2029
1.47k
    llvm::raw_svector_ostream ArgOS(Buf);
2030
1.47k
    const TemplateArgument &Argument = getArgument(Arg);
2031
1.47k
    if (Argument.getKind() == TemplateArgument::Pack) {
2032
0
      if (Argument.pack_size() && !FirstArg)
2033
0
        OS << Comma;
2034
0
      printTo(ArgOS, Argument.getPackAsArray(), Policy, true, TPL,
2035
0
              /*IsPack*/ true, ParmIndex);
2036
1.47k
    } else {
2037
1.47k
      if (!FirstArg)
2038
550
        OS << Comma;
2039
      // Tries to print the argument with location info if exists.
2040
1.47k
      printArgument(
2041
1.47k
          Arg, Policy, ArgOS,
2042
1.47k
          TemplateParameterList::shouldIncludeTypeForArgument(TPL, ParmIndex));
2043
1.47k
    }
2044
1.47k
    StringRef ArgString = ArgOS.str();
2045
2046
    // If this is the first argument and its string representation
2047
    // begins with the global scope specifier ('::foo'), add a space
2048
    // to avoid printing the diagraph '<:'.
2049
1.47k
    if (FirstArg && 
!ArgString.empty()921
&&
ArgString[0] == ':'921
)
2050
0
      OS << ' ';
2051
2052
1.47k
    OS << ArgString;
2053
2054
    // If the last character of our string is '>', add another space to
2055
    // keep the two '>''s separate tokens.
2056
1.47k
    NeedSpace = Policy.SplitTemplateClosers && 
!ArgString.empty()250
&&
2057
1.47k
                
ArgString.back() == '>'250
;
2058
1.47k
    FirstArg = false;
2059
2060
    // Use same template parameter for all elements of Pack
2061
1.47k
    if (!IsPack)
2062
1.47k
      ParmIndex++;
2063
1.47k
  }
2064
2065
923
  if (NeedSpace)
2066
3
    OS << ' ';
2067
2068
923
  if (!SkipBrackets)
2069
923
    OS << '>';
2070
923
}
2071
2072
void clang::printTemplateArgumentList(raw_ostream &OS,
2073
                                      const TemplateArgumentListInfo &Args,
2074
                                      const PrintingPolicy &Policy,
2075
0
                                      const TemplateParameterList *TPL) {
2076
0
  printTemplateArgumentList(OS, Args.arguments(), Policy, TPL);
2077
0
}
2078
2079
void clang::printTemplateArgumentList(raw_ostream &OS,
2080
                                      ArrayRef<TemplateArgument> Args,
2081
                                      const PrintingPolicy &Policy,
2082
867k
                                      const TemplateParameterList *TPL) {
2083
867k
  printTo(OS, Args, Policy, false, TPL, /*isPack*/ false, /*parmIndex*/ 0);
2084
867k
}
2085
2086
void clang::printTemplateArgumentList(raw_ostream &OS,
2087
                                      ArrayRef<TemplateArgumentLoc> Args,
2088
                                      const PrintingPolicy &Policy,
2089
923
                                      const TemplateParameterList *TPL) {
2090
923
  printTo(OS, Args, Policy, false, TPL, /*isPack*/ false, /*parmIndex*/ 0);
2091
923
}
2092
2093
2.39k
std::string Qualifiers::getAsString() const {
2094
2.39k
  LangOptions LO;
2095
2.39k
  return getAsString(PrintingPolicy(LO));
2096
2.39k
}
2097
2098
// Appends qualifiers to the given string, separated by spaces.  Will
2099
// prefix a space if the string is non-empty.  Will not append a final
2100
// space.
2101
2.39k
std::string Qualifiers::getAsString(const PrintingPolicy &Policy) const {
2102
2.39k
  SmallString<64> Buf;
2103
2.39k
  llvm::raw_svector_ostream StrOS(Buf);
2104
2.39k
  print(StrOS, Policy);
2105
2.39k
  return std::string(StrOS.str());
2106
2.39k
}
2107
2108
14.5k
bool Qualifiers::isEmptyWhenPrinted(const PrintingPolicy &Policy) const {
2109
14.5k
  if (getCVRQualifiers())
2110
11.0k
    return false;
2111
2112
3.41k
  if (getAddressSpace() != LangAS::Default)
2113
2.89k
    return false;
2114
2115
518
  if (getObjCGCAttr())
2116
45
    return false;
2117
2118
473
  if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime())
2119
470
    if (!(lifetime == Qualifiers::OCL_Strong && 
Policy.SuppressStrongLifetime351
))
2120
439
      return false;
2121
2122
34
  return true;
2123
473
}
2124
2125
91.0k
std::string Qualifiers::getAddrSpaceAsString(LangAS AS) {
2126
91.0k
  switch (AS) {
2127
80.7k
  case LangAS::Default:
2128
80.7k
    return "";
2129
1.69k
  case LangAS::opencl_global:
2130
1.73k
  case LangAS::sycl_global:
2131
1.73k
    return "__global";
2132
999
  case LangAS::opencl_local:
2133
1.01k
  case LangAS::sycl_local:
2134
1.01k
    return "__local";
2135
4.47k
  case LangAS::opencl_private:
2136
4.49k
  case LangAS::sycl_private:
2137
4.49k
    return "__private";
2138
1.16k
  case LangAS::opencl_constant:
2139
1.16k
    return "__constant";
2140
1.24k
  case LangAS::opencl_generic:
2141
1.24k
    return "__generic";
2142
52
  case LangAS::opencl_global_device:
2143
60
  case LangAS::sycl_global_device:
2144
60
    return "__global_device";
2145
52
  case LangAS::opencl_global_host:
2146
60
  case LangAS::sycl_global_host:
2147
60
    return "__global_host";
2148
0
  case LangAS::cuda_device:
2149
0
    return "__device__";
2150
0
  case LangAS::cuda_constant:
2151
0
    return "__constant__";
2152
0
  case LangAS::cuda_shared:
2153
0
    return "__shared__";
2154
0
  case LangAS::ptr32_sptr:
2155
0
    return "__sptr __ptr32";
2156
0
  case LangAS::ptr32_uptr:
2157
0
    return "__uptr __ptr32";
2158
0
  case LangAS::ptr64:
2159
0
    return "__ptr64";
2160
433
  default:
2161
433
    return std::to_string(toTargetAddressSpace(AS));
2162
91.0k
  }
2163
91.0k
}
2164
2165
// Appends qualifiers to the given string, separated by spaces.  Will
2166
// prefix a space if the string is non-empty.  Will not append a final
2167
// space.
2168
void Qualifiers::print(raw_ostream &OS, const PrintingPolicy& Policy,
2169
90.9k
                       bool appendSpaceIfNonEmpty) const {
2170
90.9k
  bool addSpace = false;
2171
2172
90.9k
  unsigned quals = getCVRQualifiers();
2173
90.9k
  if (quals) {
2174
78.8k
    AppendTypeQualList(OS, quals, Policy.Restrict);
2175
78.8k
    addSpace = true;
2176
78.8k
  }
2177
90.9k
  if (hasUnaligned()) {
2178
81
    if (addSpace)
2179
0
      OS << ' ';
2180
81
    OS << "__unaligned";
2181
81
    addSpace = true;
2182
81
  }
2183
90.9k
  auto ASStr = getAddrSpaceAsString(getAddressSpace());
2184
90.9k
  if (!ASStr.empty()) {
2185
10.1k
    if (addSpace)
2186
404
      OS << ' ';
2187
10.1k
    addSpace = true;
2188
    // Wrap target address space into an attribute syntax
2189
10.1k
    if (isTargetAddressSpace(getAddressSpace()))
2190
427
      OS << "__attribute__((address_space(" << ASStr << ")))";
2191
9.70k
    else
2192
9.70k
      OS << ASStr;
2193
10.1k
  }
2194
2195
90.9k
  if (Qualifiers::GC gc = getObjCGCAttr()) {
2196
69
    if (addSpace)
2197
0
      OS << ' ';
2198
69
    addSpace = true;
2199
69
    if (gc == Qualifiers::Weak)
2200
28
      OS << "__weak";
2201
41
    else
2202
41
      OS << "__strong";
2203
69
  }
2204
90.9k
  if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) {
2205
1.83k
    if (!(lifetime == Qualifiers::OCL_Strong && 
Policy.SuppressStrongLifetime1.10k
)){
2206
1.79k
      if (addSpace)
2207
91
        OS << ' ';
2208
1.79k
      addSpace = true;
2209
1.79k
    }
2210
2211
1.83k
    switch (lifetime) {
2212
0
    case Qualifiers::OCL_None: llvm_unreachable("none but true");
2213
68
    case Qualifiers::OCL_ExplicitNone: OS << "__unsafe_unretained"; break;
2214
1.10k
    case Qualifiers::OCL_Strong:
2215
1.10k
      if (!Policy.SuppressStrongLifetime)
2216
1.06k
        OS << "__strong";
2217
1.10k
      break;
2218
2219
510
    case Qualifiers::OCL_Weak: OS << "__weak"; break;
2220
151
    case Qualifiers::OCL_Autoreleasing: OS << "__autoreleasing"; break;
2221
1.83k
    }
2222
1.83k
  }
2223
2224
90.9k
  if (appendSpaceIfNonEmpty && 
addSpace77.9k
)
2225
77.9k
    OS << ' ';
2226
90.9k
}
2227
2228
32.7k
std::string QualType::getAsString() const {
2229
32.7k
  return getAsString(split(), LangOptions());
2230
32.7k
}
2231
2232
597k
std::string QualType::getAsString(const PrintingPolicy &Policy) const {
2233
597k
  std::string S;
2234
597k
  getAsStringInternal(S, Policy);
2235
597k
  return S;
2236
597k
}
2237
2238
std::string QualType::getAsString(const Type *ty, Qualifiers qs,
2239
90.2k
                                  const PrintingPolicy &Policy) {
2240
90.2k
  std::string buffer;
2241
90.2k
  getAsStringInternal(ty, qs, buffer, Policy);
2242
90.2k
  return buffer;
2243
90.2k
}
2244
2245
void QualType::print(raw_ostream &OS, const PrintingPolicy &Policy,
2246
1.46M
                     const Twine &PlaceHolder, unsigned Indentation) const {
2247
1.46M
  print(splitAccordingToPolicy(*this, Policy), OS, Policy, PlaceHolder,
2248
1.46M
        Indentation);
2249
1.46M
}
2250
2251
void QualType::print(const Type *ty, Qualifiers qs,
2252
                     raw_ostream &OS, const PrintingPolicy &policy,
2253
1.46M
                     const Twine &PlaceHolder, unsigned Indentation) {
2254
1.46M
  SmallString<128> PHBuf;
2255
1.46M
  StringRef PH = PlaceHolder.toStringRef(PHBuf);
2256
2257
1.46M
  TypePrinter(policy, Indentation).print(ty, qs, OS, PH);
2258
1.46M
}
2259
2260
void QualType::getAsStringInternal(std::string &Str,
2261
604k
                                   const PrintingPolicy &Policy) const {
2262
604k
  return getAsStringInternal(splitAccordingToPolicy(*this, Policy), Str,
2263
604k
                             Policy);
2264
604k
}
2265
2266
void QualType::getAsStringInternal(const Type *ty, Qualifiers qs,
2267
                                   std::string &buffer,
2268
694k
                                   const PrintingPolicy &policy) {
2269
694k
  SmallString<256> Buf;
2270
694k
  llvm::raw_svector_ostream StrOS(Buf);
2271
694k
  TypePrinter(policy).print(ty, qs, StrOS, buffer);
2272
694k
  std::string str = std::string(StrOS.str());
2273
694k
  buffer.swap(str);
2274
694k
}