Coverage Report

Created: 2021-01-19 06:58

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