Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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/PrettyPrinter.h"
14
#include "clang/AST/ASTContext.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/TemplateBase.h"
23
#include "clang/AST/TemplateName.h"
24
#include "clang/AST/Type.h"
25
#include "clang/Basic/AddressSpaces.h"
26
#include "clang/Basic/ExceptionSpecificationType.h"
27
#include "clang/Basic/IdentifierTable.h"
28
#include "clang/Basic/LLVM.h"
29
#include "clang/Basic/LangOptions.h"
30
#include "clang/Basic/SourceLocation.h"
31
#include "clang/Basic/SourceManager.h"
32
#include "clang/Basic/Specifiers.h"
33
#include "llvm/ADT/ArrayRef.h"
34
#include "llvm/ADT/SmallString.h"
35
#include "llvm/ADT/StringRef.h"
36
#include "llvm/ADT/Twine.h"
37
#include "llvm/Support/Casting.h"
38
#include "llvm/Support/Compiler.h"
39
#include "llvm/Support/ErrorHandling.h"
40
#include "llvm/Support/SaveAndRestore.h"
41
#include "llvm/Support/raw_ostream.h"
42
#include <cassert>
43
#include <string>
44
45
using namespace clang;
46
47
namespace {
48
49
  /// RAII object that enables printing of the ARC __strong lifetime
50
  /// qualifier.
51
  class IncludeStrongLifetimeRAII {
52
    PrintingPolicy &Policy;
53
    bool Old;
54
55
  public:
56
    explicit IncludeStrongLifetimeRAII(PrintingPolicy &Policy)
57
512k
        : Policy(Policy), Old(Policy.SuppressStrongLifetime) {
58
512k
        if (!Policy.SuppressLifetimeQualifiers)
59
512k
          Policy.SuppressStrongLifetime = false;
60
512k
    }
61
62
512k
    ~IncludeStrongLifetimeRAII() {
63
512k
      Policy.SuppressStrongLifetime = Old;
64
512k
    }
65
  };
66
67
  class ParamPolicyRAII {
68
    PrintingPolicy &Policy;
69
    bool Old;
70
71
  public:
72
    explicit ParamPolicyRAII(PrintingPolicy &Policy)
73
14.0k
        : Policy(Policy), Old(Policy.SuppressSpecifiers) {
74
14.0k
      Policy.SuppressSpecifiers = false;
75
14.0k
    }
76
77
14.0k
    ~ParamPolicyRAII() {
78
14.0k
      Policy.SuppressSpecifiers = Old;
79
14.0k
    }
80
  };
81
82
  class ElaboratedTypePolicyRAII {
83
    PrintingPolicy &Policy;
84
    bool SuppressTagKeyword;
85
    bool SuppressScope;
86
87
  public:
88
6.58k
    explicit ElaboratedTypePolicyRAII(PrintingPolicy &Policy) : Policy(Policy) {
89
6.58k
      SuppressTagKeyword = Policy.SuppressTagKeyword;
90
6.58k
      SuppressScope = Policy.SuppressScope;
91
6.58k
      Policy.SuppressTagKeyword = true;
92
6.58k
      Policy.SuppressScope = true;
93
6.58k
    }
94
95
6.58k
    ~ElaboratedTypePolicyRAII() {
96
6.58k
      Policy.SuppressTagKeyword = SuppressTagKeyword;
97
6.58k
      Policy.SuppressScope = SuppressScope;
98
6.58k
    }
99
  };
100
101
  class TypePrinter {
102
    PrintingPolicy Policy;
103
    unsigned Indentation;
104
    bool HasEmptyPlaceHolder = false;
105
    bool InsideCCAttribute = false;
106
107
  public:
108
    explicit TypePrinter(const PrintingPolicy &Policy, unsigned Indentation = 0)
109
736k
        : Policy(Policy), Indentation(Indentation) {}
110
111
    void print(const Type *ty, Qualifiers qs, raw_ostream &OS,
112
               StringRef PlaceHolder);
113
    void print(QualType T, raw_ostream &OS, StringRef PlaceHolder);
114
115
    static bool canPrefixQualifiers(const Type *T, bool &NeedARCStrongQualifier);
116
    void spaceBeforePlaceHolder(raw_ostream &OS);
117
    void printTypeSpec(NamedDecl *D, raw_ostream &OS);
118
119
    void printBefore(QualType T, raw_ostream &OS);
120
    void printAfter(QualType T, raw_ostream &OS);
121
    void AppendScope(DeclContext *DC, raw_ostream &OS);
122
    void printTag(TagDecl *T, raw_ostream &OS);
123
    void printFunctionAfter(const FunctionType::ExtInfo &Info, raw_ostream &OS);
124
#define ABSTRACT_TYPE(CLASS, PARENT)
125
#define TYPE(CLASS, PARENT) \
126
    void print##CLASS##Before(const CLASS##Type *T, raw_ostream &OS); \
127
    void print##CLASS##After(const CLASS##Type *T, raw_ostream &OS);
128
#include "clang/AST/TypeNodes.def"
129
130
  private:
131
    void printBefore(const Type *ty, Qualifiers qs, raw_ostream &OS);
132
    void printAfter(const Type *ty, Qualifiers qs, raw_ostream &OS);
133
  };
134
135
} // namespace
136
137
static void AppendTypeQualList(raw_ostream &OS, unsigned TypeQuals,
138
47.0k
                               bool HasRestrictKeyword) {
139
47.0k
  bool appendSpace = false;
140
47.0k
  if (TypeQuals & Qualifiers::Const) {
141
40.5k
    OS << "const";
142
40.5k
    appendSpace = true;
143
40.5k
  }
144
47.0k
  if (TypeQuals & Qualifiers::Volatile) {
145
7.16k
    if (appendSpace) 
OS << ' '725
;
146
7.16k
    OS << "volatile";
147
7.16k
    appendSpace = true;
148
7.16k
  }
149
47.0k
  if (TypeQuals & Qualifiers::Restrict) {
150
1.95k
    if (appendSpace) 
OS << ' '1.86k
;
151
1.95k
    if (HasRestrictKeyword) {
152
1.73k
      OS << "restrict";
153
1.73k
    } else {
154
227
      OS << "__restrict";
155
227
    }
156
1.95k
  }
157
47.0k
}
158
159
750k
void TypePrinter::spaceBeforePlaceHolder(raw_ostream &OS) {
160
750k
  if (!HasEmptyPlaceHolder)
161
164k
    OS << ' ';
162
750k
}
163
164
static SplitQualType splitAccordingToPolicy(QualType QT,
165
1.08M
                                            const PrintingPolicy &Policy) {
166
1.08M
  if (Policy.PrintCanonicalTypes)
167
542k
    QT = QT.getCanonicalType();
168
1.08M
  return QT.split();
169
1.08M
}
170
171
18.1k
void TypePrinter::print(QualType t, raw_ostream &OS, StringRef PlaceHolder) {
172
18.1k
  SplitQualType split = splitAccordingToPolicy(t, Policy);
173
18.1k
  print(split.Ty, split.Quals, OS, PlaceHolder);
174
18.1k
}
175
176
void TypePrinter::print(const Type *T, Qualifiers Quals, raw_ostream &OS,
177
752k
                        StringRef PlaceHolder) {
178
752k
  if (!T) {
179
37
    OS << "NULL TYPE";
180
37
    return;
181
37
  }
182
752k
183
752k
  SaveAndRestore<bool> PHVal(HasEmptyPlaceHolder, PlaceHolder.empty());
184
752k
185
752k
  printBefore(T, Quals, OS);
186
752k
  OS << PlaceHolder;
187
752k
  printAfter(T, Quals, OS);
188
752k
}
189
190
bool TypePrinter::canPrefixQualifiers(const Type *T,
191
954k
                                      bool &NeedARCStrongQualifier) {
192
954k
  // CanPrefixQualifiers - We prefer to print type qualifiers before the type,
193
954k
  // so that we get "const int" instead of "int const", but we can't do this if
194
954k
  // the type is complex.  For example if the type is "int*", we *must* print
195
954k
  // "int * const", printing "const int *" is different.  Only do this when the
196
954k
  // type expands to a simple string.
197
954k
  bool CanPrefixQualifiers = false;
198
954k
  NeedARCStrongQualifier = false;
199
954k
  Type::TypeClass TC = T->getTypeClass();
200
954k
  if (const auto *AT = dyn_cast<AutoType>(T))
201
1.41k
    TC = AT->desugar()->getTypeClass();
202
954k
  if (const auto *Subst = dyn_cast<SubstTemplateTypeParmType>(T))
203
9.46k
    TC = Subst->getReplacementType()->getTypeClass();
204
954k
205
954k
  switch (TC) {
206
954k
    case Type::Auto:
207
764k
    case Type::Builtin:
208
764k
    case Type::Complex:
209
764k
    case Type::UnresolvedUsing:
210
764k
    case Type::Typedef:
211
764k
    case Type::TypeOfExpr:
212
764k
    case Type::TypeOf:
213
764k
    case Type::Decltype:
214
764k
    case Type::UnaryTransform:
215
764k
    case Type::Record:
216
764k
    case Type::Enum:
217
764k
    case Type::Elaborated:
218
764k
    case Type::TemplateTypeParm:
219
764k
    case Type::SubstTemplateTypeParmPack:
220
764k
    case Type::DeducedTemplateSpecialization:
221
764k
    case Type::TemplateSpecialization:
222
764k
    case Type::InjectedClassName:
223
764k
    case Type::DependentName:
224
764k
    case Type::DependentTemplateSpecialization:
225
764k
    case Type::ObjCObject:
226
764k
    case Type::ObjCTypeParam:
227
764k
    case Type::ObjCInterface:
228
764k
    case Type::Atomic:
229
764k
    case Type::Pipe:
230
764k
      CanPrefixQualifiers = true;
231
764k
      break;
232
764k
233
764k
    case Type::ObjCObjectPointer:
234
9.28k
      CanPrefixQualifiers = T->isObjCIdType() || 
T->isObjCClassType()7.89k
||
235
9.28k
        
T->isObjCQualifiedIdType()7.70k
||
T->isObjCQualifiedClassType()6.69k
;
236
9.28k
      break;
237
764k
238
764k
    case Type::ConstantArray:
239
5.49k
    case Type::IncompleteArray:
240
5.49k
    case Type::VariableArray:
241
5.49k
    case Type::DependentSizedArray:
242
5.49k
      NeedARCStrongQualifier = true;
243
5.49k
      LLVM_FALLTHROUGH;
244
5.49k
245
179k
    case Type::Adjusted:
246
179k
    case Type::Decayed:
247
179k
    case Type::Pointer:
248
179k
    case Type::BlockPointer:
249
179k
    case Type::LValueReference:
250
179k
    case Type::RValueReference:
251
179k
    case Type::MemberPointer:
252
179k
    case Type::DependentAddressSpace:
253
179k
    case Type::DependentVector:
254
179k
    case Type::DependentSizedExtVector:
255
179k
    case Type::Vector:
256
179k
    case Type::ExtVector:
257
179k
    case Type::FunctionProto:
258
179k
    case Type::FunctionNoProto:
259
179k
    case Type::Paren:
260
179k
    case Type::PackExpansion:
261
179k
    case Type::SubstTemplateTypeParm:
262
179k
    case Type::MacroQualified:
263
179k
      CanPrefixQualifiers = false;
264
179k
      break;
265
179k
266
179k
    case Type::Attributed: {
267
1.21k
      // We still want to print the address_space before the type if it is an
268
1.21k
      // address_space attribute.
269
1.21k
      const auto *AttrTy = cast<AttributedType>(T);
270
1.21k
      CanPrefixQualifiers = AttrTy->getAttrKind() == attr::AddressSpace;
271
1.21k
    }
272
954k
  }
273
954k
274
954k
  return CanPrefixQualifiers;
275
954k
}
276
277
204k
void TypePrinter::printBefore(QualType T, raw_ostream &OS) {
278
204k
  SplitQualType Split = splitAccordingToPolicy(T, Policy);
279
204k
280
204k
  // If we have cv1 T, where T is substituted for cv2 U, only print cv1 - cv2
281
204k
  // at this level.
282
204k
  Qualifiers Quals = Split.Quals;
283
204k
  if (const auto *Subst = dyn_cast<SubstTemplateTypeParmType>(Split.Ty))
284
1.41k
    Quals -= QualType(Subst, 0).getQualifiers();
285
204k
286
204k
  printBefore(Split.Ty, Quals, OS);
287
204k
}
288
289
/// Prints the part of the type string before an identifier, e.g. for
290
/// "int foo[10]" it prints "int ".
291
957k
void TypePrinter::printBefore(const Type *T,Qualifiers Quals, raw_ostream &OS) {
292
957k
  if (Policy.SuppressSpecifiers && 
T->isSpecifierType()3.13k
)
293
2.96k
    return;
294
954k
295
954k
  SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder);
296
954k
297
954k
  // Print qualifiers as appropriate.
298
954k
299
954k
  bool CanPrefixQualifiers = false;
300
954k
  bool NeedARCStrongQualifier = false;
301
954k
  CanPrefixQualifiers = canPrefixQualifiers(T, NeedARCStrongQualifier);
302
954k
303
954k
  if (CanPrefixQualifiers && 
!Quals.empty()766k
) {
304
43.7k
    if (NeedARCStrongQualifier) {
305
0
      IncludeStrongLifetimeRAII Strong(Policy);
306
0
      Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/true);
307
43.7k
    } else {
308
43.7k
      Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/true);
309
43.7k
    }
310
43.7k
  }
311
954k
312
954k
  bool hasAfterQuals = false;
313
954k
  if (!CanPrefixQualifiers && 
!Quals.empty()187k
) {
314
11.5k
    hasAfterQuals = !Quals.isEmptyWhenPrinted(Policy);
315
11.5k
    if (hasAfterQuals)
316
11.5k
      HasEmptyPlaceHolder = false;
317
11.5k
  }
318
954k
319
954k
  switch (T->getTypeClass()) {
320
954k
#define ABSTRACT_TYPE(CLASS, PARENT)
321
954k
#define TYPE(CLASS, PARENT) case Type::CLASS: \
322
954k
    print##CLASS##Before(cast<CLASS##Type>(T), OS); \
323
954k
    break;
324
954k
#include 
"clang/AST/TypeNodes.def"369k
325
954k
  }
326
954k
327
954k
  if (hasAfterQuals) {
328
11.5k
    if (NeedARCStrongQualifier) {
329
308
      IncludeStrongLifetimeRAII Strong(Policy);
330
308
      Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/!PrevPHIsEmpty.get());
331
11.2k
    } else {
332
11.2k
      Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/!PrevPHIsEmpty.get());
333
11.2k
    }
334
11.5k
  }
335
954k
}
336
337
194k
void TypePrinter::printAfter(QualType t, raw_ostream &OS) {
338
194k
  SplitQualType split = splitAccordingToPolicy(t, Policy);
339
194k
  printAfter(split.Ty, split.Quals, OS);
340
194k
}
341
342
/// Prints the part of the type string after an identifier, e.g. for
343
/// "int foo[10]" it prints "[10]".
344
947k
void TypePrinter::printAfter(const Type *T, Qualifiers Quals, raw_ostream &OS) {
345
947k
  switch (T->getTypeClass()) {
346
947k
#define ABSTRACT_TYPE(CLASS, PARENT)
347
947k
#define TYPE(CLASS, PARENT) case Type::CLASS: \
348
947k
    print##CLASS##After(cast<CLASS##Type>(T), OS); \
349
947k
    break;
350
947k
#include 
"clang/AST/TypeNodes.def"369k
351
947k
  }
352
947k
}
353
354
369k
void TypePrinter::printBuiltinBefore(const BuiltinType *T, raw_ostream &OS) {
355
369k
  OS << T->getName(Policy);
356
369k
  spaceBeforePlaceHolder(OS);
357
369k
}
358
359
369k
void TypePrinter::printBuiltinAfter(const BuiltinType *T, raw_ostream &OS) {}
360
361
396
void TypePrinter::printComplexBefore(const ComplexType *T, raw_ostream &OS) {
362
396
  OS << "_Complex ";
363
396
  printBefore(T->getElementType(), OS);
364
396
}
365
366
398
void TypePrinter::printComplexAfter(const ComplexType *T, raw_ostream &OS) {
367
398
  printAfter(T->getElementType(), OS);
368
398
}
369
370
113k
void TypePrinter::printPointerBefore(const PointerType *T, raw_ostream &OS) {
371
113k
  IncludeStrongLifetimeRAII Strong(Policy);
372
113k
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
373
113k
  printBefore(T->getPointeeType(), OS);
374
113k
  // Handle things like 'int (*A)[4];' correctly.
375
113k
  // FIXME: this should include vectors, but vectors use attributes I guess.
376
113k
  if (isa<ArrayType>(T->getPointeeType()))
377
676
    OS << '(';
378
113k
  OS << '*';
379
113k
}
380
381
113k
void TypePrinter::printPointerAfter(const PointerType *T, raw_ostream &OS) {
382
113k
  IncludeStrongLifetimeRAII Strong(Policy);
383
113k
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
384
113k
  // Handle things like 'int (*A)[4];' correctly.
385
113k
  // FIXME: this should include vectors, but vectors use attributes I guess.
386
113k
  if (isa<ArrayType>(T->getPointeeType()))
387
676
    OS << ')';
388
113k
  printAfter(T->getPointeeType(), OS);
389
113k
}
390
391
void TypePrinter::printBlockPointerBefore(const BlockPointerType *T,
392
850
                                          raw_ostream &OS) {
393
850
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
394
850
  printBefore(T->getPointeeType(), OS);
395
850
  OS << '^';
396
850
}
397
398
void TypePrinter::printBlockPointerAfter(const BlockPointerType *T,
399
850
                                          raw_ostream &OS) {
400
850
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
401
850
  printAfter(T->getPointeeType(), OS);
402
850
}
403
404
// When printing a reference, the referenced type might also be a reference.
405
// If so, we want to skip that before printing the inner type.
406
51.0k
static QualType skipTopLevelReferences(QualType T) {
407
51.0k
  if (auto *Ref = T->getAs<ReferenceType>())
408
306
    return skipTopLevelReferences(Ref->getPointeeTypeAsWritten());
409
50.7k
  return T;
410
50.7k
}
411
412
void TypePrinter::printLValueReferenceBefore(const LValueReferenceType *T,
413
21.4k
                                             raw_ostream &OS) {
414
21.4k
  IncludeStrongLifetimeRAII Strong(Policy);
415
21.4k
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
416
21.4k
  QualType Inner = skipTopLevelReferences(T->getPointeeTypeAsWritten());
417
21.4k
  printBefore(Inner, OS);
418
21.4k
  // Handle things like 'int (&A)[4];' correctly.
419
21.4k
  // FIXME: this should include vectors, but vectors use attributes I guess.
420
21.4k
  if (isa<ArrayType>(Inner))
421
31
    OS << '(';
422
21.4k
  OS << '&';
423
21.4k
}
424
425
void TypePrinter::printLValueReferenceAfter(const LValueReferenceType *T,
426
21.4k
                                            raw_ostream &OS) {
427
21.4k
  IncludeStrongLifetimeRAII Strong(Policy);
428
21.4k
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
429
21.4k
  QualType Inner = skipTopLevelReferences(T->getPointeeTypeAsWritten());
430
21.4k
  // Handle things like 'int (&A)[4];' correctly.
431
21.4k
  // FIXME: this should include vectors, but vectors use attributes I guess.
432
21.4k
  if (isa<ArrayType>(Inner))
433
31
    OS << ')';
434
21.4k
  printAfter(Inner, OS);
435
21.4k
}
436
437
void TypePrinter::printRValueReferenceBefore(const RValueReferenceType *T,
438
3.88k
                                             raw_ostream &OS) {
439
3.88k
  IncludeStrongLifetimeRAII Strong(Policy);
440
3.88k
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
441
3.88k
  QualType Inner = skipTopLevelReferences(T->getPointeeTypeAsWritten());
442
3.88k
  printBefore(Inner, OS);
443
3.88k
  // Handle things like 'int (&&A)[4];' correctly.
444
3.88k
  // FIXME: this should include vectors, but vectors use attributes I guess.
445
3.88k
  if (isa<ArrayType>(Inner))
446
0
    OS << '(';
447
3.88k
  OS << "&&";
448
3.88k
}
449
450
void TypePrinter::printRValueReferenceAfter(const RValueReferenceType *T,
451
3.88k
                                            raw_ostream &OS) {
452
3.88k
  IncludeStrongLifetimeRAII Strong(Policy);
453
3.88k
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
454
3.88k
  QualType Inner = skipTopLevelReferences(T->getPointeeTypeAsWritten());
455
3.88k
  // Handle things like 'int (&&A)[4];' correctly.
456
3.88k
  // FIXME: this should include vectors, but vectors use attributes I guess.
457
3.88k
  if (isa<ArrayType>(Inner))
458
0
    OS << ')';
459
3.88k
  printAfter(Inner, OS);
460
3.88k
}
461
462
void TypePrinter::printMemberPointerBefore(const MemberPointerType *T,
463
1.46k
                                           raw_ostream &OS) {
464
1.46k
  IncludeStrongLifetimeRAII Strong(Policy);
465
1.46k
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
466
1.46k
  printBefore(T->getPointeeType(), OS);
467
1.46k
  // Handle things like 'int (Cls::*A)[4];' correctly.
468
1.46k
  // FIXME: this should include vectors, but vectors use attributes I guess.
469
1.46k
  if (isa<ArrayType>(T->getPointeeType()))
470
0
    OS << '(';
471
1.46k
472
1.46k
  PrintingPolicy InnerPolicy(Policy);
473
1.46k
  InnerPolicy.IncludeTagDefinition = false;
474
1.46k
  TypePrinter(InnerPolicy).print(QualType(T->getClass(), 0), OS, StringRef());
475
1.46k
476
1.46k
  OS << "::*";
477
1.46k
}
478
479
void TypePrinter::printMemberPointerAfter(const MemberPointerType *T,
480
1.46k
                                          raw_ostream &OS) {
481
1.46k
  IncludeStrongLifetimeRAII Strong(Policy);
482
1.46k
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
483
1.46k
  // Handle things like 'int (Cls::*A)[4];' correctly.
484
1.46k
  // FIXME: this should include vectors, but vectors use attributes I guess.
485
1.46k
  if (isa<ArrayType>(T->getPointeeType()))
486
0
    OS << ')';
487
1.46k
  printAfter(T->getPointeeType(), OS);
488
1.46k
}
489
490
void TypePrinter::printConstantArrayBefore(const ConstantArrayType *T,
491
4.62k
                                           raw_ostream &OS) {
492
4.62k
  IncludeStrongLifetimeRAII Strong(Policy);
493
4.62k
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
494
4.62k
  printBefore(T->getElementType(), OS);
495
4.62k
}
496
497
void TypePrinter::printConstantArrayAfter(const ConstantArrayType *T,
498
4.62k
                                          raw_ostream &OS) {
499
4.62k
  OS << '[';
500
4.62k
  if (T->getIndexTypeQualifiers().hasQualifiers()) {
501
1
    AppendTypeQualList(OS, T->getIndexTypeCVRQualifiers(),
502
1
                       Policy.Restrict);
503
1
    OS << ' ';
504
1
  }
505
4.62k
506
4.62k
  if (T->getSizeModifier() == ArrayType::Static)
507
2
    OS << "static ";
508
4.62k
509
4.62k
  OS << T->getSize().getZExtValue() << ']';
510
4.62k
  printAfter(T->getElementType(), OS);
511
4.62k
}
512
513
void TypePrinter::printIncompleteArrayBefore(const IncompleteArrayType *T,
514
544
                                             raw_ostream &OS) {
515
544
  IncludeStrongLifetimeRAII Strong(Policy);
516
544
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
517
544
  printBefore(T->getElementType(), OS);
518
544
}
519
520
void TypePrinter::printIncompleteArrayAfter(const IncompleteArrayType *T,
521
544
                                            raw_ostream &OS) {
522
544
  OS << "[]";
523
544
  printAfter(T->getElementType(), OS);
524
544
}
525
526
void TypePrinter::printVariableArrayBefore(const VariableArrayType *T,
527
191
                                           raw_ostream &OS) {
528
191
  IncludeStrongLifetimeRAII Strong(Policy);
529
191
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
530
191
  printBefore(T->getElementType(), OS);
531
191
}
532
533
void TypePrinter::printVariableArrayAfter(const VariableArrayType *T,
534
191
                                          raw_ostream &OS) {
535
191
  OS << '[';
536
191
  if (T->getIndexTypeQualifiers().hasQualifiers()) {
537
1
    AppendTypeQualList(OS, T->getIndexTypeCVRQualifiers(), Policy.Restrict);
538
1
    OS << ' ';
539
1
  }
540
191
541
191
  if (T->getSizeModifier() == VariableArrayType::Static)
542
2
    OS << "static ";
543
189
  else if (T->getSizeModifier() == VariableArrayType::Star)
544
8
    OS << '*';
545
191
546
191
  if (T->getSizeExpr())
547
183
    T->getSizeExpr()->printPretty(OS, nullptr, Policy);
548
191
  OS << ']';
549
191
550
191
  printAfter(T->getElementType(), OS);
551
191
}
552
553
231
void TypePrinter::printAdjustedBefore(const AdjustedType *T, raw_ostream &OS) {
554
231
  // Print the adjusted representation, otherwise the adjustment will be
555
231
  // invisible.
556
231
  printBefore(T->getAdjustedType(), OS);
557
231
}
558
559
231
void TypePrinter::printAdjustedAfter(const AdjustedType *T, raw_ostream &OS) {
560
231
  printAfter(T->getAdjustedType(), OS);
561
231
}
562
563
231
void TypePrinter::printDecayedBefore(const DecayedType *T, raw_ostream &OS) {
564
231
  // Print as though it's a pointer.
565
231
  printAdjustedBefore(T, OS);
566
231
}
567
568
231
void TypePrinter::printDecayedAfter(const DecayedType *T, raw_ostream &OS) {
569
231
  printAdjustedAfter(T, OS);
570
231
}
571
572
void TypePrinter::printDependentSizedArrayBefore(
573
                                               const DependentSizedArrayType *T,
574
110
                                               raw_ostream &OS) {
575
110
  IncludeStrongLifetimeRAII Strong(Policy);
576
110
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
577
110
  printBefore(T->getElementType(), OS);
578
110
}
579
580
void TypePrinter::printDependentSizedArrayAfter(
581
                                               const DependentSizedArrayType *T,
582
110
                                               raw_ostream &OS) {
583
110
  OS << '[';
584
110
  if (T->getSizeExpr())
585
110
    T->getSizeExpr()->printPretty(OS, nullptr, Policy);
586
110
  OS << ']';
587
110
  printAfter(T->getElementType(), OS);
588
110
}
589
590
void TypePrinter::printDependentAddressSpaceBefore(
591
1
    const DependentAddressSpaceType *T, raw_ostream &OS) {
592
1
  printBefore(T->getPointeeType(), OS);
593
1
}
594
595
void TypePrinter::printDependentAddressSpaceAfter(
596
1
    const DependentAddressSpaceType *T, raw_ostream &OS) {
597
1
  OS << " __attribute__((address_space(";
598
1
  if (T->getAddrSpaceExpr())
599
1
    T->getAddrSpaceExpr()->printPretty(OS, nullptr, Policy);
600
1
  OS << ")))";
601
1
  printAfter(T->getPointeeType(), OS);
602
1
}
603
604
void TypePrinter::printDependentSizedExtVectorBefore(
605
                                          const DependentSizedExtVectorType *T,
606
0
                                          raw_ostream &OS) {
607
0
  printBefore(T->getElementType(), OS);
608
0
}
609
610
void TypePrinter::printDependentSizedExtVectorAfter(
611
                                          const DependentSizedExtVectorType *T,
612
0
                                          raw_ostream &OS) {
613
0
  OS << " __attribute__((ext_vector_type(";
614
0
  if (T->getSizeExpr())
615
0
    T->getSizeExpr()->printPretty(OS, nullptr, Policy);
616
0
  OS << ")))";
617
0
  printAfter(T->getElementType(), OS);
618
0
}
619
620
14.5k
void TypePrinter::printVectorBefore(const VectorType *T, raw_ostream &OS) {
621
14.5k
  switch (T->getVectorKind()) {
622
14.5k
  case VectorType::AltiVecPixel:
623
0
    OS << "__vector __pixel ";
624
0
    break;
625
14.5k
  case VectorType::AltiVecBool:
626
2.62k
    OS << "__vector __bool ";
627
2.62k
    printBefore(T->getElementType(), OS);
628
2.62k
    break;
629
14.5k
  case VectorType::AltiVecVector:
630
11.3k
    OS << "__vector ";
631
11.3k
    printBefore(T->getElementType(), OS);
632
11.3k
    break;
633
14.5k
  case VectorType::NeonVector:
634
43
    OS << "__attribute__((neon_vector_type("
635
43
       << T->getNumElements() << "))) ";
636
43
    printBefore(T->getElementType(), OS);
637
43
    break;
638
14.5k
  case VectorType::NeonPolyVector:
639
0
    OS << "__attribute__((neon_polyvector_type(" <<
640
0
          T->getNumElements() << "))) ";
641
0
    printBefore(T->getElementType(), OS);
642
0
    break;
643
14.5k
  case VectorType::GenericVector: {
644
576
    // FIXME: We prefer to print the size directly here, but have no way
645
576
    // to get the size of the type.
646
576
    OS << "__attribute__((__vector_size__("
647
576
       << T->getNumElements()
648
576
       << " * sizeof(";
649
576
    print(T->getElementType(), OS, StringRef());
650
576
    OS << ")))) ";
651
576
    printBefore(T->getElementType(), OS);
652
576
    break;
653
14.5k
  }
654
14.5k
  }
655
14.5k
}
656
657
14.5k
void TypePrinter::printVectorAfter(const VectorType *T, raw_ostream &OS) {
658
14.5k
  printAfter(T->getElementType(), OS);
659
14.5k
}
660
661
void TypePrinter::printDependentVectorBefore(
662
0
    const DependentVectorType *T, raw_ostream &OS) {
663
0
  switch (T->getVectorKind()) {
664
0
  case VectorType::AltiVecPixel:
665
0
    OS << "__vector __pixel ";
666
0
    break;
667
0
  case VectorType::AltiVecBool:
668
0
    OS << "__vector __bool ";
669
0
    printBefore(T->getElementType(), OS);
670
0
    break;
671
0
  case VectorType::AltiVecVector:
672
0
    OS << "__vector ";
673
0
    printBefore(T->getElementType(), OS);
674
0
    break;
675
0
  case VectorType::NeonVector:
676
0
    OS << "__attribute__((neon_vector_type(";
677
0
    if (T->getSizeExpr())
678
0
      T->getSizeExpr()->printPretty(OS, nullptr, Policy);
679
0
    OS << "))) ";
680
0
    printBefore(T->getElementType(), OS);
681
0
    break;
682
0
  case VectorType::NeonPolyVector:
683
0
    OS << "__attribute__((neon_polyvector_type(";
684
0
    if (T->getSizeExpr())
685
0
      T->getSizeExpr()->printPretty(OS, nullptr, Policy);
686
0
    OS << "))) ";
687
0
    printBefore(T->getElementType(), OS);
688
0
    break;
689
0
  case VectorType::GenericVector: {
690
0
    // FIXME: We prefer to print the size directly here, but have no way
691
0
    // to get the size of the type.
692
0
    OS << "__attribute__((__vector_size__(";
693
0
    if (T->getSizeExpr())
694
0
      T->getSizeExpr()->printPretty(OS, nullptr, Policy);
695
0
    OS << " * sizeof(";
696
0
    print(T->getElementType(), OS, StringRef());
697
0
    OS << ")))) ";
698
0
    printBefore(T->getElementType(), OS);
699
0
    break;
700
0
  }
701
0
  }
702
0
}
703
704
void TypePrinter::printDependentVectorAfter(
705
0
    const DependentVectorType *T, raw_ostream &OS) {
706
0
  printAfter(T->getElementType(), OS);
707
0
}
708
709
void TypePrinter::printExtVectorBefore(const ExtVectorType *T,
710
758
                                       raw_ostream &OS) {
711
758
  printBefore(T->getElementType(), OS);
712
758
}
713
714
758
void TypePrinter::printExtVectorAfter(const ExtVectorType *T, raw_ostream &OS) {
715
758
  printAfter(T->getElementType(), OS);
716
758
  OS << " __attribute__((ext_vector_type(";
717
758
  OS << T->getNumElements();
718
758
  OS << ")))";
719
758
}
720
721
void
722
FunctionProtoType::printExceptionSpecification(raw_ostream &OS,
723
                                               const PrintingPolicy &Policy)
724
14.1k
                                                                         const {
725
14.1k
  if (hasDynamicExceptionSpec()) {
726
167
    OS << " throw(";
727
167
    if (getExceptionSpecType() == EST_MSAny)
728
0
      OS << "...";
729
167
    else
730
228
      
for (unsigned I = 0, N = getNumExceptions(); 167
I != N;
++I61
) {
731
61
        if (I)
732
9
          OS << ", ";
733
61
734
61
        OS << getExceptionType(I).stream(Policy);
735
61
      }
736
167
    OS << ')';
737
13.9k
  } else if (EST_NoThrow == getExceptionSpecType()) {
738
6
    OS << " __attribute__((nothrow))";
739
13.9k
  } else if (isNoexceptExceptionSpec(getExceptionSpecType())) {
740
735
    OS << " noexcept";
741
735
    // FIXME:Is it useful to print out the expression for a non-dependent
742
735
    // noexcept specification?
743
735
    if (isComputedNoexcept(getExceptionSpecType())) {
744
20
      OS << '(';
745
20
      if (getNoexceptExpr())
746
20
        getNoexceptExpr()->printPretty(OS, nullptr, Policy);
747
20
      OS << ')';
748
20
    }
749
735
  }
750
14.1k
}
751
752
void TypePrinter::printFunctionProtoBefore(const FunctionProtoType *T,
753
14.0k
                                           raw_ostream &OS) {
754
14.0k
  if (T->hasTrailingReturn()) {
755
61
    OS << "auto ";
756
61
    if (!HasEmptyPlaceHolder)
757
17
      OS << '(';
758
14.0k
  } else {
759
14.0k
    // If needed for precedence reasons, wrap the inner part in grouping parens.
760
14.0k
    SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false);
761
14.0k
    printBefore(T->getReturnType(), OS);
762
14.0k
    if (!PrevPHIsEmpty.get())
763
8.64k
      OS << '(';
764
14.0k
  }
765
14.0k
}
766
767
23
StringRef clang::getParameterABISpelling(ParameterABI ABI) {
768
23
  switch (ABI) {
769
23
  case ParameterABI::Ordinary:
770
0
    llvm_unreachable("asking for spelling of ordinary parameter ABI");
771
23
  case ParameterABI::SwiftContext:
772
6
    return "swift_context";
773
23
  case ParameterABI::SwiftErrorResult:
774
9
    return "swift_error_result";
775
23
  case ParameterABI::SwiftIndirectResult:
776
8
    return "swift_indirect_result";
777
0
  }
778
0
  llvm_unreachable("bad parameter ABI kind");
779
0
}
780
781
void TypePrinter::printFunctionProtoAfter(const FunctionProtoType *T,
782
14.0k
                                          raw_ostream &OS) {
783
14.0k
  // If needed for precedence reasons, wrap the inner part in grouping parens.
784
14.0k
  if (!HasEmptyPlaceHolder)
785
8.65k
    OS << ')';
786
14.0k
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
787
14.0k
788
14.0k
  OS << '(';
789
14.0k
  {
790
14.0k
    ParamPolicyRAII ParamPolicy(Policy);
791
27.1k
    for (unsigned i = 0, e = T->getNumParams(); i != e; 
++i13.0k
) {
792
13.0k
      if (i) 
OS << ", "3.52k
;
793
13.0k
794
13.0k
      auto EPI = T->getExtParameterInfo(i);
795
13.0k
      if (EPI.isConsumed()) 
OS << "__attribute__((ns_consumed)) "159
;
796
13.0k
      if (EPI.isNoEscape())
797
37
        OS << "__attribute__((noescape)) ";
798
13.0k
      auto ABI = EPI.getABI();
799
13.0k
      if (ABI != ParameterABI::Ordinary)
800
0
        OS << "__attribute__((" << getParameterABISpelling(ABI) << ")) ";
801
13.0k
802
13.0k
      print(T->getParamType(i), OS, StringRef());
803
13.0k
    }
804
14.0k
  }
805
14.0k
806
14.0k
  if (T->isVariadic()) {
807
295
    if (T->getNumParams())
808
272
      OS << ", ";
809
295
    OS << "...";
810
13.7k
  } else if (T->getNumParams() == 0 && 
Policy.UseVoidForZeroParams4.49k
) {
811
765
    // Do not emit int() if we have a proto, emit 'int(void)'.
812
765
    OS << "void";
813
765
  }
814
14.0k
815
14.0k
  OS << ')';
816
14.0k
817
14.0k
  FunctionType::ExtInfo Info = T->getExtInfo();
818
14.0k
819
14.0k
  printFunctionAfter(Info, OS);
820
14.0k
821
14.0k
  if (!T->getMethodQuals().empty())
822
308
    OS << " " << T->getMethodQuals().getAsString();
823
14.0k
824
14.0k
  switch (T->getRefQualifier()) {
825
14.0k
  case RQ_None:
826
13.9k
    break;
827
14.0k
828
14.0k
  case RQ_LValue:
829
37
    OS << " &";
830
37
    break;
831
14.0k
832
14.0k
  case RQ_RValue:
833
33
    OS << " &&";
834
33
    break;
835
14.0k
  }
836
14.0k
  T->printExceptionSpecification(OS, Policy);
837
14.0k
838
14.0k
  if (T->hasTrailingReturn()) {
839
61
    OS << " -> ";
840
61
    print(T->getReturnType(), OS, StringRef());
841
61
  } else
842
14.0k
    printAfter(T->getReturnType(), OS);
843
14.0k
}
844
845
void TypePrinter::printFunctionAfter(const FunctionType::ExtInfo &Info,
846
14.3k
                                     raw_ostream &OS) {
847
14.3k
  if (!InsideCCAttribute) {
848
14.1k
    switch (Info.getCC()) {
849
14.1k
    case CC_C:
850
13.6k
      // The C calling convention is the default on the vast majority of platforms
851
13.6k
      // we support.  If the user wrote it explicitly, it will usually be printed
852
13.6k
      // while traversing the AttributedType.  If the type has been desugared, let
853
13.6k
      // the canonical spelling be the implicit calling convention.
854
13.6k
      // FIXME: It would be better to be explicit in certain contexts, such as a
855
13.6k
      // cdecl function typedef used to declare a member function with the
856
13.6k
      // Microsoft C++ ABI.
857
13.6k
      break;
858
14.1k
    case CC_X86StdCall:
859
49
      OS << " __attribute__((stdcall))";
860
49
      break;
861
14.1k
    case CC_X86FastCall:
862
42
      OS << " __attribute__((fastcall))";
863
42
      break;
864
14.1k
    case CC_X86ThisCall:
865
299
      OS << " __attribute__((thiscall))";
866
299
      break;
867
14.1k
    case CC_X86VectorCall:
868
2
      OS << " __attribute__((vectorcall))";
869
2
      break;
870
14.1k
    case CC_X86Pascal:
871
0
      OS << " __attribute__((pascal))";
872
0
      break;
873
14.1k
    case CC_AAPCS:
874
0
      OS << " __attribute__((pcs(\"aapcs\")))";
875
0
      break;
876
14.1k
    case CC_AAPCS_VFP:
877
0
      OS << " __attribute__((pcs(\"aapcs-vfp\")))";
878
0
      break;
879
14.1k
    case CC_AArch64VectorCall:
880
3
      OS << "__attribute__((aarch64_vector_pcs))";
881
3
      break;
882
14.1k
    case CC_IntelOclBicc:
883
0
      OS << " __attribute__((intel_ocl_bicc))";
884
0
      break;
885
14.1k
    case CC_Win64:
886
4
      OS << " __attribute__((ms_abi))";
887
4
      break;
888
14.1k
    case CC_X86_64SysV:
889
4
      OS << " __attribute__((sysv_abi))";
890
4
      break;
891
14.1k
    case CC_X86RegCall:
892
0
      OS << " __attribute__((regcall))";
893
0
      break;
894
14.1k
    case CC_SpirFunction:
895
80
    case CC_OpenCLKernel:
896
80
      // Do nothing. These CCs are not available as attributes.
897
80
      break;
898
80
    case CC_Swift:
899
0
      OS << " __attribute__((swiftcall))";
900
0
      break;
901
80
    case CC_PreserveMost:
902
20
      OS << " __attribute__((preserve_most))";
903
20
      break;
904
80
    case CC_PreserveAll:
905
20
      OS << " __attribute__((preserve_all))";
906
20
      break;
907
14.3k
    }
908
14.3k
  }
909
14.3k
910
14.3k
  if (Info.getNoReturn())
911
69
    OS << " __attribute__((noreturn))";
912
14.3k
  if (Info.getProducesResult())
913
5
    OS << " __attribute__((ns_returns_retained))";
914
14.3k
  if (Info.getRegParm())
915
0
    OS << " __attribute__((regparm ("
916
0
       << Info.getRegParm() << ")))";
917
14.3k
  if (Info.getNoCallerSavedRegs())
918
4
    OS << " __attribute__((no_caller_saved_registers))";
919
14.3k
  if (Info.getNoCfCheck())
920
10
    OS << " __attribute__((nocf_check))";
921
14.3k
}
922
923
void TypePrinter::printFunctionNoProtoBefore(const FunctionNoProtoType *T,
924
332
                                             raw_ostream &OS) {
925
332
  // If needed for precedence reasons, wrap the inner part in grouping parens.
926
332
  SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false);
927
332
  printBefore(T->getReturnType(), OS);
928
332
  if (!PrevPHIsEmpty.get())
929
140
    OS << '(';
930
332
}
931
932
void TypePrinter::printFunctionNoProtoAfter(const FunctionNoProtoType *T,
933
332
                                            raw_ostream &OS) {
934
332
  // If needed for precedence reasons, wrap the inner part in grouping parens.
935
332
  if (!HasEmptyPlaceHolder)
936
140
    OS << ')';
937
332
  SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
938
332
939
332
  OS << "()";
940
332
  printFunctionAfter(T->getExtInfo(), OS);
941
332
  printAfter(T->getReturnType(), OS);
942
332
}
943
944
15.3k
void TypePrinter::printTypeSpec(NamedDecl *D, raw_ostream &OS) {
945
15.3k
946
15.3k
  // Compute the full nested-name-specifier for this type.
947
15.3k
  // In C, this will always be empty except when the type
948
15.3k
  // being printed is anonymous within other Record.
949
15.3k
  if (!Policy.SuppressScope)
950
14.5k
    AppendScope(D->getDeclContext(), OS);
951
15.3k
952
15.3k
  IdentifierInfo *II = D->getIdentifier();
953
15.3k
  OS << II->getName();
954
15.3k
  spaceBeforePlaceHolder(OS);
955
15.3k
}
956
957
void TypePrinter::printUnresolvedUsingBefore(const UnresolvedUsingType *T,
958
0
                                             raw_ostream &OS) {
959
0
  printTypeSpec(T->getDecl(), OS);
960
0
}
961
962
void TypePrinter::printUnresolvedUsingAfter(const UnresolvedUsingType *T,
963
0
                                            raw_ostream &OS) {}
964
965
15.3k
void TypePrinter::printTypedefBefore(const TypedefType *T, raw_ostream &OS) {
966
15.3k
  printTypeSpec(T->getDecl(), OS);
967
15.3k
}
968
969
void TypePrinter::printMacroQualifiedBefore(const MacroQualifiedType *T,
970
33
                                            raw_ostream &OS) {
971
33
  StringRef MacroName = T->getMacroIdentifier()->getName();
972
33
  OS << MacroName << " ";
973
33
974
33
  // Since this type is meant to print the macro instead of the whole attribute,
975
33
  // we trim any attributes and go directly to the original modified type.
976
33
  printBefore(T->getModifiedType(), OS);
977
33
}
978
979
void TypePrinter::printMacroQualifiedAfter(const MacroQualifiedType *T,
980
33
                                           raw_ostream &OS) {
981
33
  printAfter(T->getModifiedType(), OS);
982
33
}
983
984
15.3k
void TypePrinter::printTypedefAfter(const TypedefType *T, raw_ostream &OS) {}
985
986
void TypePrinter::printTypeOfExprBefore(const TypeOfExprType *T,
987
79
                                        raw_ostream &OS) {
988
79
  OS << "typeof ";
989
79
  if (T->getUnderlyingExpr())
990
79
    T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy);
991
79
  spaceBeforePlaceHolder(OS);
992
79
}
993
994
void TypePrinter::printTypeOfExprAfter(const TypeOfExprType *T,
995
79
                                       raw_ostream &OS) {}
996
997
5
void TypePrinter::printTypeOfBefore(const TypeOfType *T, raw_ostream &OS) {
998
5
  OS << "typeof(";
999
5
  print(T->getUnderlyingType(), OS, StringRef());
1000
5
  OS << ')';
1001
5
  spaceBeforePlaceHolder(OS);
1002
5
}
1003
1004
5
void TypePrinter::printTypeOfAfter(const TypeOfType *T, raw_ostream &OS) {}
1005
1006
156
void TypePrinter::printDecltypeBefore(const DecltypeType *T, raw_ostream &OS) {
1007
156
  OS << "decltype(";
1008
156
  if (T->getUnderlyingExpr())
1009
156
    T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy);
1010
156
  OS << ')';
1011
156
  spaceBeforePlaceHolder(OS);
1012
156
}
1013
1014
156
void TypePrinter::printDecltypeAfter(const DecltypeType *T, raw_ostream &OS) {}
1015
1016
void TypePrinter::printUnaryTransformBefore(const UnaryTransformType *T,
1017
4
                                            raw_ostream &OS) {
1018
4
  IncludeStrongLifetimeRAII Strong(Policy);
1019
4
1020
4
  switch (T->getUTTKind()) {
1021
4
    case UnaryTransformType::EnumUnderlyingType:
1022
4
      OS << "__underlying_type(";
1023
4
      print(T->getBaseType(), OS, StringRef());
1024
4
      OS << ')';
1025
4
      spaceBeforePlaceHolder(OS);
1026
4
      return;
1027
0
  }
1028
0
1029
0
  printBefore(T->getBaseType(), OS);
1030
0
}
1031
1032
void TypePrinter::printUnaryTransformAfter(const UnaryTransformType *T,
1033
4
                                           raw_ostream &OS) {
1034
4
  IncludeStrongLifetimeRAII Strong(Policy);
1035
4
1036
4
  switch (T->getUTTKind()) {
1037
4
    case UnaryTransformType::EnumUnderlyingType:
1038
4
      return;
1039
0
  }
1040
0
1041
0
  printAfter(T->getBaseType(), OS);
1042
0
}
1043
1044
1.41k
void TypePrinter::printAutoBefore(const AutoType *T, raw_ostream &OS) {
1045
1.41k
  // If the type has been deduced, do not print 'auto'.
1046
1.41k
  if (!T->getDeducedType().isNull()) {
1047
532
    printBefore(T->getDeducedType(), OS);
1048
887
  } else {
1049
887
    switch (T->getKeyword()) {
1050
887
    
case AutoTypeKeyword::Auto: OS << "auto"; break835
;
1051
887
    
case AutoTypeKeyword::DecltypeAuto: OS << "decltype(auto)"; break50
;
1052
887
    
case AutoTypeKeyword::GNUAutoType: OS << "__auto_type"; break2
;
1053
887
    }
1054
887
    spaceBeforePlaceHolder(OS);
1055
887
  }
1056
1.41k
}
1057
1058
1.41k
void TypePrinter::printAutoAfter(const AutoType *T, raw_ostream &OS) {
1059
1.41k
  // If the type has been deduced, do not print 'auto'.
1060
1.41k
  if (!T->getDeducedType().isNull())
1061
532
    printAfter(T->getDeducedType(), OS);
1062
1.41k
}
1063
1064
void TypePrinter::printDeducedTemplateSpecializationBefore(
1065
101
    const DeducedTemplateSpecializationType *T, raw_ostream &OS) {
1066
101
  // If the type has been deduced, print the deduced type.
1067
101
  if (!T->getDeducedType().isNull()) {
1068
19
    printBefore(T->getDeducedType(), OS);
1069
82
  } else {
1070
82
    IncludeStrongLifetimeRAII Strong(Policy);
1071
82
    T->getTemplateName().print(OS, Policy);
1072
82
    spaceBeforePlaceHolder(OS);
1073
82
  }
1074
101
}
1075
1076
void TypePrinter::printDeducedTemplateSpecializationAfter(
1077
101
    const DeducedTemplateSpecializationType *T, raw_ostream &OS) {
1078
101
  // If the type has been deduced, print the deduced type.
1079
101
  if (!T->getDeducedType().isNull())
1080
19
    printAfter(T->getDeducedType(), OS);
1081
101
}
1082
1083
235
void TypePrinter::printAtomicBefore(const AtomicType *T, raw_ostream &OS) {
1084
235
  IncludeStrongLifetimeRAII Strong(Policy);
1085
235
1086
235
  OS << "_Atomic(";
1087
235
  print(T->getValueType(), OS, StringRef());
1088
235
  OS << ')';
1089
235
  spaceBeforePlaceHolder(OS);
1090
235
}
1091
1092
235
void TypePrinter::printAtomicAfter(const AtomicType *T, raw_ostream &OS) {}
1093
1094
96
void TypePrinter::printPipeBefore(const PipeType *T, raw_ostream &OS) {
1095
96
  IncludeStrongLifetimeRAII Strong(Policy);
1096
96
1097
96
  if (T->isReadOnly())
1098
74
    OS << "read_only ";
1099
22
  else
1100
22
    OS << "write_only ";
1101
96
  OS << "pipe ";
1102
96
  print(T->getElementType(), OS, StringRef());
1103
96
  spaceBeforePlaceHolder(OS);
1104
96
}
1105
1106
96
void TypePrinter::printPipeAfter(const PipeType *T, raw_ostream &OS) {}
1107
1108
/// Appends the given scope to the end of a string.
1109
867k
void TypePrinter::AppendScope(DeclContext *DC, raw_ostream &OS) {
1110
867k
  if (DC->isTranslationUnit()) 
return347k
;
1111
520k
  if (DC->isFunctionOrMethod()) 
return3.68k
;
1112
516k
  AppendScope(DC->getParent(), OS);
1113
516k
1114
516k
  if (const auto *NS = dyn_cast<NamespaceDecl>(DC)) {
1115
500k
    if (Policy.SuppressUnwrittenScope &&
1116
500k
        
(7
NS->isAnonymousNamespace()7
||
NS->isInline()2
))
1117
5
      return;
1118
500k
    if (NS->getIdentifier())
1119
500k
      OS << NS->getName() << "::";
1120
217
    else
1121
217
      OS << "(anonymous namespace)::";
1122
500k
  } else 
if (const auto *15.5k
Spec15.5k
= dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
1123
6.81k
    IncludeStrongLifetimeRAII Strong(Policy);
1124
6.81k
    OS << Spec->getIdentifier()->getName();
1125
6.81k
    const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1126
6.81k
    printTemplateArgumentList(OS, TemplateArgs.asArray(), Policy);
1127
6.81k
    OS << "::";
1128
8.78k
  } else if (const auto *Tag = dyn_cast<TagDecl>(DC)) {
1129
8.49k
    if (TypedefNameDecl *Typedef = Tag->getTypedefNameForAnonDecl())
1130
2
      OS << Typedef->getIdentifier()->getName() << "::";
1131
8.49k
    else if (Tag->getIdentifier())
1132
8.38k
      OS << Tag->getIdentifier()->getName() << "::";
1133
111
    else
1134
111
      return;
1135
8.49k
  }
1136
516k
}
1137
1138
342k
void TypePrinter::printTag(TagDecl *D, raw_ostream &OS) {
1139
342k
  if (Policy.IncludeTagDefinition) {
1140
0
    PrintingPolicy SubPolicy = Policy;
1141
0
    SubPolicy.IncludeTagDefinition = false;
1142
0
    D->print(OS, SubPolicy, Indentation);
1143
0
    spaceBeforePlaceHolder(OS);
1144
0
    return;
1145
0
  }
1146
342k
1147
342k
  bool HasKindDecoration = false;
1148
342k
1149
342k
  // We don't print tags unless this is an elaborated type.
1150
342k
  // In C, we just assume every RecordType is an elaborated type.
1151
342k
  if (!Policy.SuppressTagKeyword && 
!D->getTypedefNameForAnonDecl()12.3k
) {
1152
12.0k
    HasKindDecoration = true;
1153
12.0k
    OS << D->getKindName();
1154
12.0k
    OS << ' ';
1155
12.0k
  }
1156
342k
1157
342k
  // Compute the full nested-name-specifier for this type.
1158
342k
  // In C, this will always be empty except when the type
1159
342k
  // being printed is anonymous within other Record.
1160
342k
  if (!Policy.SuppressScope)
1161
336k
    AppendScope(D->getDeclContext(), OS);
1162
342k
1163
342k
  if (const IdentifierInfo *II = D->getIdentifier())
1164
339k
    OS << II->getName();
1165
2.61k
  else if (TypedefNameDecl *Typedef = D->getTypedefNameForAnonDecl()) {
1166
385
    assert(Typedef->getIdentifier() && "Typedef without identifier?");
1167
385
    OS << Typedef->getIdentifier()->getName();
1168
2.23k
  } else {
1169
2.23k
    // Make an unambiguous representation for anonymous types, e.g.
1170
2.23k
    //   (anonymous enum at /usr/include/string.h:120:9)
1171
2.23k
    OS << (Policy.MSVCFormatting ? 
'`'5
:
'('2.22k
);
1172
2.23k
1173
2.23k
    if (isa<CXXRecordDecl>(D) && 
cast<CXXRecordDecl>(D)->isLambda()1.27k
) {
1174
1.09k
      OS << "lambda";
1175
1.09k
      HasKindDecoration = true;
1176
1.13k
    } else {
1177
1.13k
      OS << "anonymous";
1178
1.13k
    }
1179
2.23k
1180
2.23k
    if (Policy.AnonymousTagLocations) {
1181
2.23k
      // Suppress the redundant tag keyword if we just printed one.
1182
2.23k
      // We don't have to worry about ElaboratedTypes here because you can't
1183
2.23k
      // refer to an anonymous type with one.
1184
2.23k
      if (!HasKindDecoration)
1185
330
        OS << " " << D->getKindName();
1186
2.23k
1187
2.23k
      PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc(
1188
2.23k
          D->getLocation());
1189
2.23k
      if (PLoc.isValid()) {
1190
2.23k
        OS << " at ";
1191
2.23k
        StringRef File = PLoc.getFilename();
1192
2.23k
        if (Policy.RemapFilePaths)
1193
211
          OS << Policy.remapPath(File);
1194
2.01k
        else
1195
2.01k
          OS << File;
1196
2.23k
        OS << ':' << PLoc.getLine() << ':' << PLoc.getColumn();
1197
2.23k
      }
1198
2.23k
    }
1199
2.23k
1200
2.23k
    OS << (Policy.MSVCFormatting ? 
'\''5
:
')'2.22k
);
1201
2.23k
  }
1202
342k
1203
342k
  // If this is a class template specialization, print the template
1204
342k
  // arguments.
1205
342k
  if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
1206
194k
    ArrayRef<TemplateArgument> Args;
1207
194k
    if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
1208
37.8k
      const TemplateSpecializationType *TST =
1209
37.8k
        cast<TemplateSpecializationType>(TAW->getType());
1210
37.8k
      Args = TST->template_arguments();
1211
156k
    } else {
1212
156k
      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1213
156k
      Args = TemplateArgs.asArray();
1214
156k
    }
1215
194k
    IncludeStrongLifetimeRAII Strong(Policy);
1216
194k
    printTemplateArgumentList(OS, Args, Policy);
1217
194k
  }
1218
342k
1219
342k
  spaceBeforePlaceHolder(OS);
1220
342k
}
1221
1222
337k
void TypePrinter::printRecordBefore(const RecordType *T, raw_ostream &OS) {
1223
337k
  printTag(T->getDecl(), OS);
1224
337k
}
1225
1226
337k
void TypePrinter::printRecordAfter(const RecordType *T, raw_ostream &OS) {}
1227
1228
5.54k
void TypePrinter::printEnumBefore(const EnumType *T, raw_ostream &OS) {
1229
5.54k
  printTag(T->getDecl(), OS);
1230
5.54k
}
1231
1232
5.54k
void TypePrinter::printEnumAfter(const EnumType *T, raw_ostream &OS) {}
1233
1234
void TypePrinter::printTemplateTypeParmBefore(const TemplateTypeParmType *T,
1235
6.40k
                                              raw_ostream &OS) {
1236
6.40k
  if (IdentifierInfo *Id = T->getIdentifier())
1237
5.89k
    OS << Id->getName();
1238
510
  else {
1239
510
    bool IsLambdaAutoParam = false;
1240
510
    if (auto D = T->getDecl()) {
1241
31
      if (auto M = dyn_cast_or_null<CXXMethodDecl>(D->getDeclContext()))
1242
24
        IsLambdaAutoParam = D->isImplicit() && 
M->getParent()->isLambda()21
;
1243
31
    }
1244
510
1245
510
    if (IsLambdaAutoParam)
1246
21
      OS << "auto";
1247
489
    else
1248
489
      OS << "type-parameter-" << T->getDepth() << '-' << T->getIndex();
1249
510
  }
1250
6.40k
  spaceBeforePlaceHolder(OS);
1251
6.40k
}
1252
1253
void TypePrinter::printTemplateTypeParmAfter(const TemplateTypeParmType *T,
1254
7.13k
                                             raw_ostream &OS) {}
1255
1256
void TypePrinter::printSubstTemplateTypeParmBefore(
1257
                                             const SubstTemplateTypeParmType *T,
1258
9.46k
                                             raw_ostream &OS) {
1259
9.46k
  IncludeStrongLifetimeRAII Strong(Policy);
1260
9.46k
  printBefore(T->getReplacementType(), OS);
1261
9.46k
}
1262
1263
void TypePrinter::printSubstTemplateTypeParmAfter(
1264
                                             const SubstTemplateTypeParmType *T,
1265
10.4k
                                             raw_ostream &OS) {
1266
10.4k
  IncludeStrongLifetimeRAII Strong(Policy);
1267
10.4k
  printAfter(T->getReplacementType(), OS);
1268
10.4k
}
1269
1270
void TypePrinter::printSubstTemplateTypeParmPackBefore(
1271
                                        const SubstTemplateTypeParmPackType *T,
1272
8
                                        raw_ostream &OS) {
1273
8
  IncludeStrongLifetimeRAII Strong(Policy);
1274
8
  printTemplateTypeParmBefore(T->getReplacedParameter(), OS);
1275
8
}
1276
1277
void TypePrinter::printSubstTemplateTypeParmPackAfter(
1278
                                        const SubstTemplateTypeParmPackType *T,
1279
8
                                        raw_ostream &OS) {
1280
8
  IncludeStrongLifetimeRAII Strong(Policy);
1281
8
  printTemplateTypeParmAfter(T->getReplacedParameter(), OS);
1282
8
}
1283
1284
void TypePrinter::printTemplateSpecializationBefore(
1285
                                            const TemplateSpecializationType *T,
1286
4.93k
                                            raw_ostream &OS) {
1287
4.93k
  IncludeStrongLifetimeRAII Strong(Policy);
1288
4.93k
  T->getTemplateName().print(OS, Policy);
1289
4.93k
1290
4.93k
  printTemplateArgumentList(OS, T->template_arguments(), Policy);
1291
4.93k
  spaceBeforePlaceHolder(OS);
1292
4.93k
}
1293
1294
void TypePrinter::printTemplateSpecializationAfter(
1295
                                            const TemplateSpecializationType *T,
1296
4.21k
                                            raw_ostream &OS) {}
1297
1298
void TypePrinter::printInjectedClassNameBefore(const InjectedClassNameType *T,
1299
728
                                               raw_ostream &OS) {
1300
728
  printTemplateSpecializationBefore(T->getInjectedTST(), OS);
1301
728
}
1302
1303
void TypePrinter::printInjectedClassNameAfter(const InjectedClassNameType *T,
1304
728
                                               raw_ostream &OS) {}
1305
1306
void TypePrinter::printElaboratedBefore(const ElaboratedType *T,
1307
3.52k
                                        raw_ostream &OS) {
1308
3.52k
  if (Policy.IncludeTagDefinition && 
T->getOwnedTagDecl()244
) {
1309
244
    TagDecl *OwnedTagDecl = T->getOwnedTagDecl();
1310
244
    assert(OwnedTagDecl->getTypeForDecl() == T->getNamedType().getTypePtr() &&
1311
244
           "OwnedTagDecl expected to be a declaration for the type");
1312
244
    PrintingPolicy SubPolicy = Policy;
1313
244
    SubPolicy.IncludeTagDefinition = false;
1314
244
    OwnedTagDecl->print(OS, SubPolicy, Indentation);
1315
244
    spaceBeforePlaceHolder(OS);
1316
244
    return;
1317
244
  }
1318
3.28k
1319
3.28k
  // The tag definition will take care of these.
1320
3.28k
  if (!Policy.IncludeTagDefinition)
1321
3.28k
  {
1322
3.28k
    OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1323
3.28k
    if (T->getKeyword() != ETK_None)
1324
2.42k
      OS << " ";
1325
3.28k
    NestedNameSpecifier *Qualifier = T->getQualifier();
1326
3.28k
    if (Qualifier)
1327
1.11k
      Qualifier->print(OS, Policy);
1328
3.28k
  }
1329
3.28k
1330
3.28k
  ElaboratedTypePolicyRAII PolicyRAII(Policy);
1331
3.28k
  printBefore(T->getNamedType(), OS);
1332
3.28k
}
1333
1334
void TypePrinter::printElaboratedAfter(const ElaboratedType *T,
1335
3.54k
                                        raw_ostream &OS) {
1336
3.54k
  if (Policy.IncludeTagDefinition && 
T->getOwnedTagDecl()244
)
1337
244
    return;
1338
3.30k
  ElaboratedTypePolicyRAII PolicyRAII(Policy);
1339
3.30k
  printAfter(T->getNamedType(), OS);
1340
3.30k
}
1341
1342
1.85k
void TypePrinter::printParenBefore(const ParenType *T, raw_ostream &OS) {
1343
1.85k
  if (!HasEmptyPlaceHolder && 
!isa<FunctionType>(T->getInnerType())1.72k
) {
1344
128
    printBefore(T->getInnerType(), OS);
1345
128
    OS << '(';
1346
128
  } else
1347
1.73k
    printBefore(T->getInnerType(), OS);
1348
1.85k
}
1349
1350
1.85k
void TypePrinter::printParenAfter(const ParenType *T, raw_ostream &OS) {
1351
1.85k
  if (!HasEmptyPlaceHolder && 
!isa<FunctionType>(T->getInnerType())1.72k
) {
1352
128
    OS << ')';
1353
128
    printAfter(T->getInnerType(), OS);
1354
128
  } else
1355
1.73k
    printAfter(T->getInnerType(), OS);
1356
1.85k
}
1357
1358
void TypePrinter::printDependentNameBefore(const DependentNameType *T,
1359
130
                                           raw_ostream &OS) {
1360
130
  OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1361
130
  if (T->getKeyword() != ETK_None)
1362
123
    OS << " ";
1363
130
1364
130
  T->getQualifier()->print(OS, Policy);
1365
130
1366
130
  OS << T->getIdentifier()->getName();
1367
130
  spaceBeforePlaceHolder(OS);
1368
130
}
1369
1370
void TypePrinter::printDependentNameAfter(const DependentNameType *T,
1371
130
                                          raw_ostream &OS) {}
1372
1373
void TypePrinter::printDependentTemplateSpecializationBefore(
1374
16
        const DependentTemplateSpecializationType *T, raw_ostream &OS) {
1375
16
  IncludeStrongLifetimeRAII Strong(Policy);
1376
16
1377
16
  OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1378
16
  if (T->getKeyword() != ETK_None)
1379
16
    OS << " ";
1380
16
1381
16
  if (T->getQualifier())
1382
8
    T->getQualifier()->print(OS, Policy);
1383
16
  OS << T->getIdentifier()->getName();
1384
16
  printTemplateArgumentList(OS, T->template_arguments(), Policy);
1385
16
  spaceBeforePlaceHolder(OS);
1386
16
}
1387
1388
void TypePrinter::printDependentTemplateSpecializationAfter(
1389
16
        const DependentTemplateSpecializationType *T, raw_ostream &OS) {}
1390
1391
void TypePrinter::printPackExpansionBefore(const PackExpansionType *T,
1392
111
                                           raw_ostream &OS) {
1393
111
  printBefore(T->getPattern(), OS);
1394
111
}
1395
1396
void TypePrinter::printPackExpansionAfter(const PackExpansionType *T,
1397
111
                                          raw_ostream &OS) {
1398
111
  printAfter(T->getPattern(), OS);
1399
111
  OS << "...";
1400
111
}
1401
1402
void TypePrinter::printAttributedBefore(const AttributedType *T,
1403
1.21k
                                        raw_ostream &OS) {
1404
1.21k
  // FIXME: Generate this with TableGen.
1405
1.21k
1406
1.21k
  // Prefer the macro forms of the GC and ownership qualifiers.
1407
1.21k
  if (T->getAttrKind() == attr::ObjCGC ||
1408
1.21k
      
T->getAttrKind() == attr::ObjCOwnership1.17k
)
1409
463
    return printBefore(T->getEquivalentType(), OS);
1410
751
1411
751
  if (T->getAttrKind() == attr::ObjCKindOf)
1412
76
    OS << "__kindof ";
1413
751
1414
751
  if (T->getAttrKind() == attr::AddressSpace)
1415
59
    printBefore(T->getEquivalentType(), OS);
1416
692
  else
1417
692
    printBefore(T->getModifiedType(), OS);
1418
751
1419
751
  if (T->isMSTypeSpec()) {
1420
11
    switch (T->getAttrKind()) {
1421
11
    
default: return0
;
1422
11
    
case attr::Ptr32: OS << " __ptr32"; break5
;
1423
11
    
case attr::Ptr64: OS << " __ptr64"; break2
;
1424
11
    
case attr::SPtr: OS << " __sptr"; break2
;
1425
11
    
case attr::UPtr: OS << " __uptr"; break2
;
1426
11
    }
1427
11
    spaceBeforePlaceHolder(OS);
1428
11
  }
1429
751
1430
751
  // Print nullability type specifiers.
1431
751
  if (T->getImmediateNullability()) {
1432
367
    if (T->getAttrKind() == attr::TypeNonNull)
1433
152
      OS << " _Nonnull";
1434
215
    else if (T->getAttrKind() == attr::TypeNullable)
1435
199
      OS << " _Nullable";
1436
16
    else if (T->getAttrKind() == attr::TypeNullUnspecified)
1437
16
      OS << " _Null_unspecified";
1438
16
    else
1439
16
      
llvm_unreachable0
("unhandled nullability");
1440
367
    spaceBeforePlaceHolder(OS);
1441
367
  }
1442
751
}
1443
1444
void TypePrinter::printAttributedAfter(const AttributedType *T,
1445
1.15k
                                       raw_ostream &OS) {
1446
1.15k
  // FIXME: Generate this with TableGen.
1447
1.15k
1448
1.15k
  // Prefer the macro forms of the GC and ownership qualifiers.
1449
1.15k
  if (T->getAttrKind() == attr::ObjCGC ||
1450
1.15k
      
T->getAttrKind() == attr::ObjCOwnership1.11k
)
1451
463
    return printAfter(T->getEquivalentType(), OS);
1452
692
1453
692
  // If this is a calling convention attribute, don't print the implicit CC from
1454
692
  // the modified type.
1455
692
  SaveAndRestore<bool> MaybeSuppressCC(InsideCCAttribute, T->isCallingConv());
1456
692
1457
692
  printAfter(T->getModifiedType(), OS);
1458
692
1459
692
  // Some attributes are printed as qualifiers before the type, so we have
1460
692
  // nothing left to do.
1461
692
  if (T->getAttrKind() == attr::ObjCKindOf ||
1462
692
      
T->isMSTypeSpec()675
||
T->getImmediateNullability()664
)
1463
395
    return;
1464
297
1465
297
  // Don't print the inert __unsafe_unretained attribute at all.
1466
297
  if (T->getAttrKind() == attr::ObjCInertUnsafeUnretained)
1467
2
    return;
1468
295
1469
295
  // Don't print ns_returns_retained unless it had an effect.
1470
295
  if (T->getAttrKind() == attr::NSReturnsRetained &&
1471
295
      !T->getEquivalentType()->castAs<FunctionType>()
1472
3
                             ->getExtInfo().getProducesResult())
1473
0
    return;
1474
295
1475
295
  if (T->getAttrKind() == attr::LifetimeBound) {
1476
0
    OS << " [[clang::lifetimebound]]";
1477
0
    return;
1478
0
  }
1479
295
1480
295
  // The printing of the address_space attribute is handled by the qualifier
1481
295
  // since it is still stored in the qualifier. Return early to prevent printing
1482
295
  // this twice.
1483
295
  if (T->getAttrKind() == attr::AddressSpace)
1484
59
    return;
1485
236
1486
236
  OS << " __attribute__((";
1487
236
  switch (T->getAttrKind()) {
1488
236
#define TYPE_ATTR(NAME)
1489
236
#define DECL_OR_TYPE_ATTR(NAME)
1490
236
#define ATTR(NAME) 
case attr::NAME:0
1491
236
#include "clang/Basic/AttrList.inc"
1492
0
    llvm_unreachable("non-type attribute attached to type");
1493
0
1494
0
  case attr::OpenCLPrivateAddressSpace:
1495
0
  case attr::OpenCLGlobalAddressSpace:
1496
0
  case attr::OpenCLLocalAddressSpace:
1497
0
  case attr::OpenCLConstantAddressSpace:
1498
0
  case attr::OpenCLGenericAddressSpace:
1499
0
    // FIXME: Update printAttributedBefore to print these once we generate
1500
0
    // AttributedType nodes for them.
1501
0
    break;
1502
0
1503
0
  case attr::LifetimeBound:
1504
0
  case attr::TypeNonNull:
1505
0
  case attr::TypeNullable:
1506
0
  case attr::TypeNullUnspecified:
1507
0
  case attr::ObjCGC:
1508
0
  case attr::ObjCInertUnsafeUnretained:
1509
0
  case attr::ObjCKindOf:
1510
0
  case attr::ObjCOwnership:
1511
0
  case attr::Ptr32:
1512
0
  case attr::Ptr64:
1513
0
  case attr::SPtr:
1514
0
  case attr::UPtr:
1515
0
  case attr::AddressSpace:
1516
0
    llvm_unreachable("This attribute should have been handled already");
1517
0
1518
3
  case attr::NSReturnsRetained:
1519
3
    OS << "ns_returns_retained";
1520
3
    break;
1521
0
1522
0
  // FIXME: When Sema learns to form this AttributedType, avoid printing the
1523
0
  // attribute again in printFunctionProtoAfter.
1524
0
  case attr::AnyX86NoCfCheck: OS << "nocf_check"; break;
1525
97
  case attr::CDecl: OS << "cdecl"; break;
1526
25
  case attr::FastCall: OS << "fastcall"; break;
1527
26
  case attr::StdCall: OS << "stdcall"; break;
1528
27
  case attr::ThisCall: OS << "thiscall"; break;
1529
0
  case attr::SwiftCall: OS << "swiftcall"; break;
1530
2
  case attr::VectorCall: OS << "vectorcall"; break;
1531
0
  case attr::Pascal: OS << "pascal"; break;
1532
6
  case attr::MSABI: OS << "ms_abi"; break;
1533
6
  case attr::SysVABI: OS << "sysv_abi"; break;
1534
0
  case attr::RegCall: OS << "regcall"; break;
1535
0
  case attr::Pcs: {
1536
0
    OS << "pcs(";
1537
0
   QualType t = T->getEquivalentType();
1538
0
   while (!t->isFunctionType())
1539
0
     t = t->getPointeeType();
1540
0
   OS << (t->getAs<FunctionType>()->getCallConv() == CC_AAPCS ?
1541
0
         "\"aapcs\"" : "\"aapcs-vfp\"");
1542
0
   OS << ')';
1543
0
   break;
1544
0
  }
1545
0
  case attr::AArch64VectorPcs: OS << "aarch64_vector_pcs"; break;
1546
0
  case attr::IntelOclBicc: OS << "inteloclbicc"; break;
1547
20
  case attr::PreserveMost:
1548
20
    OS << "preserve_most";
1549
20
    break;
1550
0
1551
20
  case attr::PreserveAll:
1552
20
    OS << "preserve_all";
1553
20
    break;
1554
4
  case attr::NoDeref:
1555
4
    OS << "noderef";
1556
4
    break;
1557
236
  }
1558
236
  OS << "))";
1559
236
}
1560
1561
void TypePrinter::printObjCInterfaceBefore(const ObjCInterfaceType *T,
1562
7.50k
                                           raw_ostream &OS) {
1563
7.50k
  OS << T->getDecl()->getName();
1564
7.50k
  spaceBeforePlaceHolder(OS);
1565
7.50k
}
1566
1567
void TypePrinter::printObjCInterfaceAfter(const ObjCInterfaceType *T,
1568
1.75k
                                          raw_ostream &OS) {}
1569
1570
void TypePrinter::printObjCTypeParamBefore(const ObjCTypeParamType *T,
1571
15
                                          raw_ostream &OS) {
1572
15
  OS << T->getDecl()->getName();
1573
15
  if (!T->qual_empty()) {
1574
0
    bool isFirst = true;
1575
0
    OS << '<';
1576
0
    for (const auto *I : T->quals()) {
1577
0
      if (isFirst)
1578
0
        isFirst = false;
1579
0
      else
1580
0
        OS << ',';
1581
0
      OS << I->getName();
1582
0
    }
1583
0
    OS << '>';
1584
0
  }
1585
15
1586
15
  spaceBeforePlaceHolder(OS);
1587
15
}
1588
1589
void TypePrinter::printObjCTypeParamAfter(const ObjCTypeParamType *T,
1590
15
                                          raw_ostream &OS) {}
1591
1592
void TypePrinter::printObjCObjectBefore(const ObjCObjectType *T,
1593
3.58k
                                        raw_ostream &OS) {
1594
3.58k
  if (T->qual_empty() && 
T->isUnspecializedAsWritten()2.36k
&&
1595
3.58k
      
!T->isKindOfTypeAsWritten()1.68k
)
1596
1.62k
    return printBefore(T->getBaseType(), OS);
1597
1.95k
1598
1.95k
  if (T->isKindOfTypeAsWritten())
1599
68
    OS << "__kindof ";
1600
1.95k
1601
1.95k
  print(T->getBaseType(), OS, StringRef());
1602
1.95k
1603
1.95k
  if (T->isSpecializedAsWritten()) {
1604
687
    bool isFirst = true;
1605
687
    OS << '<';
1606
716
    for (auto typeArg : T->getTypeArgsAsWritten()) {
1607
716
      if (isFirst)
1608
687
        isFirst = false;
1609
29
      else
1610
29
        OS << ",";
1611
716
1612
716
      print(typeArg, OS, StringRef());
1613
716
    }
1614
687
    OS << '>';
1615
687
  }
1616
1.95k
1617
1.95k
  if (!T->qual_empty()) {
1618
1.22k
    bool isFirst = true;
1619
1.22k
    OS << '<';
1620
2.05k
    for (const auto *I : T->quals()) {
1621
2.05k
      if (isFirst)
1622
1.22k
        isFirst = false;
1623
835
      else
1624
835
        OS << ',';
1625
2.05k
      OS << I->getName();
1626
2.05k
    }
1627
1.22k
    OS << '>';
1628
1.22k
  }
1629
1.95k
1630
1.95k
  spaceBeforePlaceHolder(OS);
1631
1.95k
}
1632
1633
void TypePrinter::printObjCObjectAfter(const ObjCObjectType *T,
1634
89
                                        raw_ostream &OS) {
1635
89
  if (T->qual_empty() && 
T->isUnspecializedAsWritten()63
&&
1636
89
      
!T->isKindOfTypeAsWritten()53
)
1637
52
    return printAfter(T->getBaseType(), OS);
1638
89
}
1639
1640
void TypePrinter::printObjCObjectPointerBefore(const ObjCObjectPointerType *T,
1641
9.25k
                                               raw_ostream &OS) {
1642
9.25k
  printBefore(T->getPointeeType(), OS);
1643
9.25k
1644
9.25k
  // If we need to print the pointer, print it now.
1645
9.25k
  if (!T->isObjCIdType() && 
!T->isObjCQualifiedIdType()7.86k
&&
1646
9.25k
      
!T->isObjCClassType()6.86k
&&
!T->isObjCQualifiedClassType()6.67k
) {
1647
6.66k
    if (HasEmptyPlaceHolder)
1648
5.82k
      OS << ' ';
1649
6.66k
    OS << '*';
1650
6.66k
  }
1651
9.25k
}
1652
1653
void TypePrinter::printObjCObjectPointerAfter(const ObjCObjectPointerType *T,
1654
9.25k
                                              raw_ostream &OS) {}
1655
1656
static
1657
576k
const TemplateArgument &getArgument(const TemplateArgument &A) { return A; }
1658
1659
671
static const TemplateArgument &getArgument(const TemplateArgumentLoc &A) {
1660
671
  return A.getArgument();
1661
671
}
1662
1663
static void printArgument(const TemplateArgument &A, const PrintingPolicy &PP,
1664
570k
                          llvm::raw_ostream &OS) {
1665
570k
  A.print(PP, OS);
1666
570k
}
1667
1668
static void printArgument(const TemplateArgumentLoc &A,
1669
671
                          const PrintingPolicy &PP, llvm::raw_ostream &OS) {
1670
671
  const TemplateArgument::ArgKind &Kind = A.getArgument().getKind();
1671
671
  if (Kind == TemplateArgument::ArgKind::Type)
1672
468
    return A.getTypeSourceInfo()->getType().print(OS, PP);
1673
203
  return A.getArgument().print(PP, OS);
1674
203
}
1675
1676
template<typename TA>
1677
static void printTo(raw_ostream &OS, ArrayRef<TA> Args,
1678
355k
                    const PrintingPolicy &Policy, bool SkipBrackets) {
1679
355k
  const char *Comma = Policy.MSVCFormatting ? 
","38
:
", "355k
;
1680
355k
  if (!SkipBrackets)
1681
349k
    OS << '<';
1682
355k
1683
355k
  bool NeedSpace = false;
1684
355k
  bool FirstArg = true;
1685
577k
  for (const auto &Arg : Args) {
1686
577k
    // Print the argument into a string.
1687
577k
    SmallString<128> Buf;
1688
577k
    llvm::raw_svector_ostream ArgOS(Buf);
1689
577k
    const TemplateArgument &Argument = getArgument(Arg);
1690
577k
    if (Argument.getKind() == TemplateArgument::Pack) {
1691
6.00k
      if (Argument.pack_size() && 
!FirstArg5.53k
)
1692
2.82k
        OS << Comma;
1693
6.00k
      printTo(ArgOS, Argument.getPackAsArray(), Policy, true);
1694
571k
    } else {
1695
571k
      if (!FirstArg)
1696
219k
        OS << Comma;
1697
571k
      // Tries to print the argument with location info if exists.
1698
571k
      printArgument(Arg, Policy, ArgOS);
1699
571k
    }
1700
577k
    StringRef ArgString = ArgOS.str();
1701
577k
1702
577k
    // If this is the first argument and its string representation
1703
577k
    // begins with the global scope specifier ('::foo'), add a space
1704
577k
    // to avoid printing the diagraph '<:'.
1705
577k
    if (FirstArg && 
!ArgString.empty()355k
&&
ArgString[0] == ':'355k
)
1706
0
      OS << ' ';
1707
577k
1708
577k
    OS << ArgString;
1709
577k
1710
577k
    NeedSpace = (!ArgString.empty() && 
ArgString.back() == '>'577k
);
1711
577k
    FirstArg = false;
1712
577k
  }
1713
355k
1714
355k
  // If the last character of our string is '>', add another space to
1715
355k
  // keep the two '>''s separate tokens. We don't *have* to do this in
1716
355k
  // C++0x, but it's still good hygiene.
1717
355k
  if (NeedSpace)
1718
88.3k
    OS << ' ';
1719
355k
1720
355k
  if (!SkipBrackets)
1721
349k
    OS << '>';
1722
355k
}
TypePrinter.cpp:void printTo<clang::TemplateArgumentLoc>(llvm::raw_ostream&, llvm::ArrayRef<clang::TemplateArgumentLoc>, clang::PrintingPolicy const&, bool)
Line
Count
Source
1678
473
                    const PrintingPolicy &Policy, bool SkipBrackets) {
1679
473
  const char *Comma = Policy.MSVCFormatting ? 
","0
: ", ";
1680
473
  if (!SkipBrackets)
1681
473
    OS << '<';
1682
473
1683
473
  bool NeedSpace = false;
1684
473
  bool FirstArg = true;
1685
671
  for (const auto &Arg : Args) {
1686
671
    // Print the argument into a string.
1687
671
    SmallString<128> Buf;
1688
671
    llvm::raw_svector_ostream ArgOS(Buf);
1689
671
    const TemplateArgument &Argument = getArgument(Arg);
1690
671
    if (Argument.getKind() == TemplateArgument::Pack) {
1691
0
      if (Argument.pack_size() && !FirstArg)
1692
0
        OS << Comma;
1693
0
      printTo(ArgOS, Argument.getPackAsArray(), Policy, true);
1694
671
    } else {
1695
671
      if (!FirstArg)
1696
200
        OS << Comma;
1697
671
      // Tries to print the argument with location info if exists.
1698
671
      printArgument(Arg, Policy, ArgOS);
1699
671
    }
1700
671
    StringRef ArgString = ArgOS.str();
1701
671
1702
671
    // If this is the first argument and its string representation
1703
671
    // begins with the global scope specifier ('::foo'), add a space
1704
671
    // to avoid printing the diagraph '<:'.
1705
671
    if (FirstArg && 
!ArgString.empty()471
&&
ArgString[0] == ':'471
)
1706
0
      OS << ' ';
1707
671
1708
671
    OS << ArgString;
1709
671
1710
671
    NeedSpace = (!ArgString.empty() && ArgString.back() == '>');
1711
671
    FirstArg = false;
1712
671
  }
1713
473
1714
473
  // If the last character of our string is '>', add another space to
1715
473
  // keep the two '>''s separate tokens. We don't *have* to do this in
1716
473
  // C++0x, but it's still good hygiene.
1717
473
  if (NeedSpace)
1718
1
    OS << ' ';
1719
473
1720
473
  if (!SkipBrackets)
1721
473
    OS << '>';
1722
473
}
TypePrinter.cpp:void printTo<clang::TemplateArgument>(llvm::raw_ostream&, llvm::ArrayRef<clang::TemplateArgument>, clang::PrintingPolicy const&, bool)
Line
Count
Source
1678
355k
                    const PrintingPolicy &Policy, bool SkipBrackets) {
1679
355k
  const char *Comma = Policy.MSVCFormatting ? 
","38
:
", "355k
;
1680
355k
  if (!SkipBrackets)
1681
349k
    OS << '<';
1682
355k
1683
355k
  bool NeedSpace = false;
1684
355k
  bool FirstArg = true;
1685
576k
  for (const auto &Arg : Args) {
1686
576k
    // Print the argument into a string.
1687
576k
    SmallString<128> Buf;
1688
576k
    llvm::raw_svector_ostream ArgOS(Buf);
1689
576k
    const TemplateArgument &Argument = getArgument(Arg);
1690
576k
    if (Argument.getKind() == TemplateArgument::Pack) {
1691
6.00k
      if (Argument.pack_size() && 
!FirstArg5.53k
)
1692
2.82k
        OS << Comma;
1693
6.00k
      printTo(ArgOS, Argument.getPackAsArray(), Policy, true);
1694
570k
    } else {
1695
570k
      if (!FirstArg)
1696
218k
        OS << Comma;
1697
570k
      // Tries to print the argument with location info if exists.
1698
570k
      printArgument(Arg, Policy, ArgOS);
1699
570k
    }
1700
576k
    StringRef ArgString = ArgOS.str();
1701
576k
1702
576k
    // If this is the first argument and its string representation
1703
576k
    // begins with the global scope specifier ('::foo'), add a space
1704
576k
    // to avoid printing the diagraph '<:'.
1705
576k
    if (FirstArg && 
!ArgString.empty()354k
&&
ArgString[0] == ':'354k
)
1706
0
      OS << ' ';
1707
576k
1708
576k
    OS << ArgString;
1709
576k
1710
576k
    NeedSpace = (!ArgString.empty() && 
ArgString.back() == '>'576k
);
1711
576k
    FirstArg = false;
1712
576k
  }
1713
355k
1714
355k
  // If the last character of our string is '>', add another space to
1715
355k
  // keep the two '>''s separate tokens. We don't *have* to do this in
1716
355k
  // C++0x, but it's still good hygiene.
1717
355k
  if (NeedSpace)
1718
88.3k
    OS << ' ';
1719
355k
1720
355k
  if (!SkipBrackets)
1721
349k
    OS << '>';
1722
355k
}
1723
1724
void clang::printTemplateArgumentList(raw_ostream &OS,
1725
                                      const TemplateArgumentListInfo &Args,
1726
0
                                      const PrintingPolicy &Policy) {
1727
0
  return printTo(OS, Args.arguments(), Policy, false);
1728
0
}
1729
1730
void clang::printTemplateArgumentList(raw_ostream &OS,
1731
                                      ArrayRef<TemplateArgument> Args,
1732
349k
                                      const PrintingPolicy &Policy) {
1733
349k
  printTo(OS, Args, Policy, false);
1734
349k
}
1735
1736
void clang::printTemplateArgumentList(raw_ostream &OS,
1737
                                      ArrayRef<TemplateArgumentLoc> Args,
1738
473
                                      const PrintingPolicy &Policy) {
1739
473
  printTo(OS, Args, Policy, false);
1740
473
}
1741
1742
844
std::string Qualifiers::getAsString() const {
1743
844
  LangOptions LO;
1744
844
  return getAsString(PrintingPolicy(LO));
1745
844
}
1746
1747
// Appends qualifiers to the given string, separated by spaces.  Will
1748
// prefix a space if the string is non-empty.  Will not append a final
1749
// space.
1750
844
std::string Qualifiers::getAsString(const PrintingPolicy &Policy) const {
1751
844
  SmallString<64> Buf;
1752
844
  llvm::raw_svector_ostream StrOS(Buf);
1753
844
  print(StrOS, Policy);
1754
844
  return StrOS.str();
1755
844
}
1756
1757
11.5k
bool Qualifiers::isEmptyWhenPrinted(const PrintingPolicy &Policy) const {
1758
11.5k
  if (getCVRQualifiers())
1759
8.90k
    return false;
1760
2.64k
1761
2.64k
  if (getAddressSpace() != LangAS::Default)
1762
2.21k
    return false;
1763
432
1764
432
  if (getObjCGCAttr())
1765
45
    return false;
1766
387
1767
387
  if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime())
1768
384
    if (!(lifetime == Qualifiers::OCL_Strong && 
Policy.SuppressStrongLifetime270
))
1769
353
      return false;
1770
34
1771
34
  return true;
1772
34
}
1773
1774
// Appends qualifiers to the given string, separated by spaces.  Will
1775
// prefix a space if the string is non-empty.  Will not append a final
1776
// space.
1777
void Qualifiers::print(raw_ostream &OS, const PrintingPolicy& Policy,
1778
56.3k
                       bool appendSpaceIfNonEmpty) const {
1779
56.3k
  bool addSpace = false;
1780
56.3k
1781
56.3k
  unsigned quals = getCVRQualifiers();
1782
56.3k
  if (quals) {
1783
47.0k
    AppendTypeQualList(OS, quals, Policy.Restrict);
1784
47.0k
    addSpace = true;
1785
47.0k
  }
1786
56.3k
  if (hasUnaligned()) {
1787
81
    if (addSpace)
1788
0
      OS << ' ';
1789
81
    OS << "__unaligned";
1790
81
    addSpace = true;
1791
81
  }
1792
56.3k
  LangAS addrspace = getAddressSpace();
1793
56.3k
  if (addrspace != LangAS::Default) {
1794
7.56k
    if (addrspace != LangAS::opencl_private) {
1795
3.35k
      if (addSpace)
1796
139
        OS << ' ';
1797
3.35k
      addSpace = true;
1798
3.35k
      switch (addrspace) {
1799
3.35k
      case LangAS::opencl_global:
1800
1.04k
        OS << "__global";
1801
1.04k
        break;
1802
3.35k
      case LangAS::opencl_local:
1803
633
        OS << "__local";
1804
633
        break;
1805
3.35k
      case LangAS::opencl_private:
1806
0
        break;
1807
3.35k
      case LangAS::opencl_constant:
1808
676
        OS << "__constant";
1809
676
        break;
1810
3.35k
      case LangAS::opencl_generic:
1811
558
        OS << "__generic";
1812
558
        break;
1813
3.35k
      case LangAS::cuda_device:
1814
0
        OS << "__device__";
1815
0
        break;
1816
3.35k
      case LangAS::cuda_constant:
1817
0
        OS << "__constant__";
1818
0
        break;
1819
3.35k
      case LangAS::cuda_shared:
1820
0
        OS << "__shared__";
1821
0
        break;
1822
3.35k
      default:
1823
445
        OS << "__attribute__((address_space(";
1824
445
        OS << toTargetAddressSpace(addrspace);
1825
445
        OS << ")))";
1826
3.35k
      }
1827
3.35k
    }
1828
7.56k
  }
1829
56.3k
  if (Qualifiers::GC gc = getObjCGCAttr()) {
1830
69
    if (addSpace)
1831
0
      OS << ' ';
1832
69
    addSpace = true;
1833
69
    if (gc == Qualifiers::Weak)
1834
28
      OS << "__weak";
1835
41
    else
1836
41
      OS << "__strong";
1837
69
  }
1838
56.3k
  if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) {
1839
1.65k
    if (!(lifetime == Qualifiers::OCL_Strong && 
Policy.SuppressStrongLifetime958
)){
1840
1.61k
      if (addSpace)
1841
91
        OS << ' ';
1842
1.61k
      addSpace = true;
1843
1.61k
    }
1844
1.65k
1845
1.65k
    switch (lifetime) {
1846
1.65k
    
case Qualifiers::OCL_None: 0
llvm_unreachable0
("none but true");
1847
1.65k
    
case Qualifiers::OCL_ExplicitNone: OS << "__unsafe_unretained"; break60
;
1848
1.65k
    case Qualifiers::OCL_Strong:
1849
958
      if (!Policy.SuppressStrongLifetime)
1850
920
        OS << "__strong";
1851
958
      break;
1852
1.65k
1853
1.65k
    
case Qualifiers::OCL_Weak: OS << "__weak"; break494
;
1854
1.65k
    
case Qualifiers::OCL_Autoreleasing: OS << "__autoreleasing"; break142
;
1855
56.3k
    }
1856
56.3k
  }
1857
56.3k
1858
56.3k
  if (appendSpaceIfNonEmpty && 
addSpace45.7k
)
1859
43.2k
    OS << ' ';
1860
56.3k
}
1861
1862
31.6k
std::string QualType::getAsString() const {
1863
31.6k
  return getAsString(split(), LangOptions());
1864
31.6k
}
1865
1866
171k
std::string QualType::getAsString(const PrintingPolicy &Policy) const {
1867
171k
  std::string S;
1868
171k
  getAsStringInternal(S, Policy);
1869
171k
  return S;
1870
171k
}
1871
1872
std::string QualType::getAsString(const Type *ty, Qualifiers qs,
1873
64.5k
                                  const PrintingPolicy &Policy) {
1874
64.5k
  std::string buffer;
1875
64.5k
  getAsStringInternal(ty, qs, buffer, Policy);
1876
64.5k
  return buffer;
1877
64.5k
}
1878
1879
void QualType::print(raw_ostream &OS, const PrintingPolicy &Policy,
1880
491k
                     const Twine &PlaceHolder, unsigned Indentation) const {
1881
491k
  print(splitAccordingToPolicy(*this, Policy), OS, Policy, PlaceHolder,
1882
491k
        Indentation);
1883
491k
}
1884
1885
void QualType::print(const Type *ty, Qualifiers qs,
1886
                     raw_ostream &OS, const PrintingPolicy &policy,
1887
491k
                     const Twine &PlaceHolder, unsigned Indentation) {
1888
491k
  SmallString<128> PHBuf;
1889
491k
  StringRef PH = PlaceHolder.toStringRef(PHBuf);
1890
491k
1891
491k
  TypePrinter(policy, Indentation).print(ty, qs, OS, PH);
1892
491k
}
1893
1894
void QualType::getAsStringInternal(std::string &Str,
1895
178k
                                   const PrintingPolicy &Policy) const {
1896
178k
  return getAsStringInternal(splitAccordingToPolicy(*this, Policy), Str,
1897
178k
                             Policy);
1898
178k
}
1899
1900
void QualType::getAsStringInternal(const Type *ty, Qualifiers qs,
1901
                                   std::string &buffer,
1902
243k
                                   const PrintingPolicy &policy) {
1903
243k
  SmallString<256> Buf;
1904
243k
  llvm::raw_svector_ostream StrOS(Buf);
1905
243k
  TypePrinter(policy).print(ty, qs, StrOS, buffer);
1906
243k
  std::string str = StrOS.str();
1907
243k
  buffer.swap(str);
1908
243k
}