Coverage Report

Created: 2022-01-18 06:27

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