1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
|
//===--- TypePrinter.cpp - Pretty-Print Clang Types -----------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This contains code to print types from Clang's type system.
//
//===----------------------------------------------------------------------===//
#include "clang/AST/Decl.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/Expr.h"
#include "clang/AST/Type.h"
#include "clang/AST/PrettyPrinter.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/SourceManager.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/raw_ostream.h"
using namespace clang;
namespace {
class TypePrinter {
PrintingPolicy Policy;
public:
explicit TypePrinter(const PrintingPolicy &Policy) : Policy(Policy) { }
void Print(QualType T, std::string &S);
void AppendScope(DeclContext *DC, std::string &S);
void PrintTag(TagDecl *T, std::string &S);
#define ABSTRACT_TYPE(CLASS, PARENT)
#define TYPE(CLASS, PARENT) \
void Print##CLASS(const CLASS##Type *T, std::string &S);
#include "clang/AST/TypeNodes.def"
};
}
static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
if (TypeQuals & Qualifiers::Const) {
if (!S.empty()) S += ' ';
S += "const";
}
if (TypeQuals & Qualifiers::Volatile) {
if (!S.empty()) S += ' ';
S += "volatile";
}
if (TypeQuals & Qualifiers::Restrict) {
if (!S.empty()) S += ' ';
S += "restrict";
}
}
void TypePrinter::Print(QualType T, std::string &S) {
if (T.isNull()) {
S += "NULL TYPE";
return;
}
if (Policy.SuppressSpecifiers && T->isSpecifierType())
return;
// Print qualifiers as appropriate.
Qualifiers Quals = T.getLocalQualifiers();
if (!Quals.empty()) {
std::string TQS;
Quals.getAsStringInternal(TQS, Policy);
if (!S.empty()) {
TQS += ' ';
TQS += S;
}
std::swap(S, TQS);
}
switch (T->getTypeClass()) {
#define ABSTRACT_TYPE(CLASS, PARENT)
#define TYPE(CLASS, PARENT) case Type::CLASS: \
Print##CLASS(cast<CLASS##Type>(T.getTypePtr()), S); \
break;
#include "clang/AST/TypeNodes.def"
}
}
void TypePrinter::PrintBuiltin(const BuiltinType *T, std::string &S) {
if (S.empty()) {
S = T->getName(Policy.LangOpts);
} else {
// Prefix the basic type, e.g. 'int X'.
S = ' ' + S;
S = T->getName(Policy.LangOpts) + S;
}
}
void TypePrinter::PrintComplex(const ComplexType *T, std::string &S) {
Print(T->getElementType(), S);
S = "_Complex " + S;
}
void TypePrinter::PrintPointer(const PointerType *T, std::string &S) {
S = '*' + S;
// Handle things like 'int (*A)[4];' correctly.
// FIXME: this should include vectors, but vectors use attributes I guess.
if (isa<ArrayType>(T->getPointeeType()))
S = '(' + S + ')';
Print(T->getPointeeType(), S);
}
void TypePrinter::PrintBlockPointer(const BlockPointerType *T, std::string &S) {
S = '^' + S;
Print(T->getPointeeType(), S);
}
void TypePrinter::PrintLValueReference(const LValueReferenceType *T,
std::string &S) {
S = '&' + S;
// Handle things like 'int (&A)[4];' correctly.
// FIXME: this should include vectors, but vectors use attributes I guess.
if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
S = '(' + S + ')';
Print(T->getPointeeTypeAsWritten(), S);
}
void TypePrinter::PrintRValueReference(const RValueReferenceType *T,
std::string &S) {
S = "&&" + S;
// Handle things like 'int (&&A)[4];' correctly.
// FIXME: this should include vectors, but vectors use attributes I guess.
if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
S = '(' + S + ')';
Print(T->getPointeeTypeAsWritten(), S);
}
void TypePrinter::PrintMemberPointer(const MemberPointerType *T,
std::string &S) {
std::string C;
Print(QualType(T->getClass(), 0), C);
C += "::*";
S = C + S;
// Handle things like 'int (Cls::*A)[4];' correctly.
// FIXME: this should include vectors, but vectors use attributes I guess.
if (isa<ArrayType>(T->getPointeeType()))
S = '(' + S + ')';
Print(T->getPointeeType(), S);
}
void TypePrinter::PrintConstantArray(const ConstantArrayType *T,
std::string &S) {
S += '[';
S += llvm::utostr(T->getSize().getZExtValue());
S += ']';
Print(T->getElementType(), S);
}
void TypePrinter::PrintIncompleteArray(const IncompleteArrayType *T,
std::string &S) {
S += "[]";
Print(T->getElementType(), S);
}
void TypePrinter::PrintVariableArray(const VariableArrayType *T,
std::string &S) {
S += '[';
if (T->getIndexTypeQualifiers().hasQualifiers()) {
AppendTypeQualList(S, T->getIndexTypeCVRQualifiers());
S += ' ';
}
if (T->getSizeModifier() == VariableArrayType::Static)
S += "static";
else if (T->getSizeModifier() == VariableArrayType::Star)
S += '*';
if (T->getSizeExpr()) {
std::string SStr;
llvm::raw_string_ostream s(SStr);
T->getSizeExpr()->printPretty(s, 0, Policy);
S += s.str();
}
S += ']';
Print(T->getElementType(), S);
}
void TypePrinter::PrintDependentSizedArray(const DependentSizedArrayType *T,
std::string &S) {
S += '[';
if (T->getSizeExpr()) {
std::string SStr;
llvm::raw_string_ostream s(SStr);
T->getSizeExpr()->printPretty(s, 0, Policy);
S += s.str();
}
S += ']';
Print(T->getElementType(), S);
}
void TypePrinter::PrintDependentSizedExtVector(
const DependentSizedExtVectorType *T,
std::string &S) {
Print(T->getElementType(), S);
S += " __attribute__((ext_vector_type(";
if (T->getSizeExpr()) {
std::string SStr;
llvm::raw_string_ostream s(SStr);
T->getSizeExpr()->printPretty(s, 0, Policy);
S += s.str();
}
S += ")))";
}
void TypePrinter::PrintVector(const VectorType *T, std::string &S) {
if (T->isAltiVec()) {
if (T->isPixel())
S = "__vector __pixel " + S;
else {
Print(T->getElementType(), S);
S = "__vector " + S;
}
} else {
// FIXME: We prefer to print the size directly here, but have no way
// to get the size of the type.
Print(T->getElementType(), S);
std::string V = "__attribute__((__vector_size__(";
V += llvm::utostr_32(T->getNumElements()); // convert back to bytes.
std::string ET;
Print(T->getElementType(), ET);
V += " * sizeof(" + ET + ")))) ";
S = V + S;
}
}
void TypePrinter::PrintExtVector(const ExtVectorType *T, std::string &S) {
S += " __attribute__((ext_vector_type(";
S += llvm::utostr_32(T->getNumElements());
S += ")))";
Print(T->getElementType(), S);
}
void TypePrinter::PrintFunctionProto(const FunctionProtoType *T,
std::string &S) {
// If needed for precedence reasons, wrap the inner part in grouping parens.
if (!S.empty())
S = "(" + S + ")";
S += "(";
std::string Tmp;
PrintingPolicy ParamPolicy(Policy);
ParamPolicy.SuppressSpecifiers = false;
for (unsigned i = 0, e = T->getNumArgs(); i != e; ++i) {
if (i) S += ", ";
Print(T->getArgType(i), Tmp);
S += Tmp;
Tmp.clear();
}
if (T->isVariadic()) {
if (T->getNumArgs())
S += ", ";
S += "...";
} else if (T->getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) {
// Do not emit int() if we have a proto, emit 'int(void)'.
S += "void";
}
S += ")";
FunctionType::ExtInfo Info = T->getExtInfo();
switch(Info.getCC()) {
case CC_Default:
default: break;
case CC_C:
S += " __attribute__((cdecl))";
break;
case CC_X86StdCall:
S += " __attribute__((stdcall))";
break;
case CC_X86FastCall:
S += " __attribute__((fastcall))";
break;
}
if (Info.getNoReturn())
S += " __attribute__((noreturn))";
if (Info.getRegParm())
S += " __attribute__((regparm (" +
llvm::utostr_32(Info.getRegParm()) + ")))";
if (T->hasExceptionSpec()) {
S += " throw(";
if (T->hasAnyExceptionSpec())
S += "...";
else
for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I) {
if (I)
S += ", ";
std::string ExceptionType;
Print(T->getExceptionType(I), ExceptionType);
S += ExceptionType;
}
S += ")";
}
AppendTypeQualList(S, T->getTypeQuals());
Print(T->getResultType(), S);
}
void TypePrinter::PrintFunctionNoProto(const FunctionNoProtoType *T,
std::string &S) {
// If needed for precedence reasons, wrap the inner part in grouping parens.
if (!S.empty())
S = "(" + S + ")";
S += "()";
if (T->getNoReturnAttr())
S += " __attribute__((noreturn))";
Print(T->getResultType(), S);
}
static void PrintTypeSpec(const NamedDecl *D, std::string &S) {
IdentifierInfo *II = D->getIdentifier();
if (S.empty())
S = II->getName().str();
else
S = II->getName().str() + ' ' + S;
}
void TypePrinter::PrintUnresolvedUsing(const UnresolvedUsingType *T,
std::string &S) {
PrintTypeSpec(T->getDecl(), S);
}
void TypePrinter::PrintTypedef(const TypedefType *T, std::string &S) {
PrintTypeSpec(T->getDecl(), S);
}
void TypePrinter::PrintTypeOfExpr(const TypeOfExprType *T, std::string &S) {
if (!S.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
S = ' ' + S;
std::string Str;
llvm::raw_string_ostream s(Str);
T->getUnderlyingExpr()->printPretty(s, 0, Policy);
S = "typeof " + s.str() + S;
}
void TypePrinter::PrintTypeOf(const TypeOfType *T, std::string &S) {
if (!S.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
S = ' ' + S;
std::string Tmp;
Print(T->getUnderlyingType(), Tmp);
S = "typeof(" + Tmp + ")" + S;
}
void TypePrinter::PrintDecltype(const DecltypeType *T, std::string &S) {
if (!S.empty()) // Prefix the basic type, e.g. 'decltype(t) X'.
S = ' ' + S;
std::string Str;
llvm::raw_string_ostream s(Str);
T->getUnderlyingExpr()->printPretty(s, 0, Policy);
S = "decltype(" + s.str() + ")" + S;
}
/// Appends the given scope to the end of a string.
void TypePrinter::AppendScope(DeclContext *DC, std::string &Buffer) {
if (DC->isTranslationUnit()) return;
AppendScope(DC->getParent(), Buffer);
unsigned OldSize = Buffer.size();
if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
if (NS->getIdentifier())
Buffer += NS->getNameAsString();
else
Buffer += "<anonymous>";
} else if (ClassTemplateSpecializationDecl *Spec
= dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
std::string TemplateArgsStr
= TemplateSpecializationType::PrintTemplateArgumentList(
TemplateArgs.getFlatArgumentList(),
TemplateArgs.flat_size(),
Policy);
Buffer += Spec->getIdentifier()->getName();
Buffer += TemplateArgsStr;
} else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
Buffer += Typedef->getIdentifier()->getName();
else if (Tag->getIdentifier())
Buffer += Tag->getIdentifier()->getName();
}
if (Buffer.size() != OldSize)
Buffer += "::";
}
void TypePrinter::PrintTag(TagDecl *D, std::string &InnerString) {
if (Policy.SuppressTag)
return;
std::string Buffer;
bool HasKindDecoration = false;
// We don't print tags unless this is an elaborated type.
// In C, we just assume every RecordType is an elaborated type.
if (!Policy.LangOpts.CPlusPlus && !D->getTypedefForAnonDecl()) {
HasKindDecoration = true;
Buffer += D->getKindName();
Buffer += ' ';
}
if (!Policy.SuppressScope)
// Compute the full nested-name-specifier for this type. In C,
// this will always be empty.
AppendScope(D->getDeclContext(), Buffer);
if (const IdentifierInfo *II = D->getIdentifier())
Buffer += II->getNameStart();
else if (TypedefDecl *Typedef = D->getTypedefForAnonDecl()) {
assert(Typedef->getIdentifier() && "Typedef without identifier?");
Buffer += Typedef->getIdentifier()->getNameStart();
} else {
// Make an unambiguous representation for anonymous types, e.g.
// <anonymous enum at /usr/include/string.h:120:9>
llvm::raw_string_ostream OS(Buffer);
OS << "<anonymous";
if (Policy.AnonymousTagLocations) {
// Suppress the redundant tag keyword if we just printed one.
// We don't have to worry about ElaboratedTypes here because you can't
// refer to an anonymous type with one.
if (!HasKindDecoration)
OS << " " << D->getKindName();
PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc(
D->getLocation());
OS << " at " << PLoc.getFilename()
<< ':' << PLoc.getLine()
<< ':' << PLoc.getColumn();
}
OS << '>';
OS.flush();
}
// If this is a class template specialization, print the template
// arguments.
if (ClassTemplateSpecializationDecl *Spec
= dyn_cast<ClassTemplateSpecializationDecl>(D)) {
const TemplateArgument *Args;
unsigned NumArgs;
if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
const TemplateSpecializationType *TST =
cast<TemplateSpecializationType>(TAW->getType());
Args = TST->getArgs();
NumArgs = TST->getNumArgs();
} else {
const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Args = TemplateArgs.getFlatArgumentList();
NumArgs = TemplateArgs.flat_size();
}
Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
NumArgs,
Policy);
}
if (!InnerString.empty()) {
Buffer += ' ';
Buffer += InnerString;
}
std::swap(Buffer, InnerString);
}
void TypePrinter::PrintRecord(const RecordType *T, std::string &S) {
PrintTag(T->getDecl(), S);
}
void TypePrinter::PrintEnum(const EnumType *T, std::string &S) {
PrintTag(T->getDecl(), S);
}
void TypePrinter::PrintElaborated(const ElaboratedType *T, std::string &S) {
Print(T->getUnderlyingType(), S);
// We don't actually make these in C, but the language options
// sometimes lie to us -- for example, if someone calls
// QualType::getAsString(). Just suppress the redundant tag if so.
if (Policy.LangOpts.CPlusPlus)
S = std::string(T->getNameForTagKind(T->getTagKind())) + ' ' + S;
}
void TypePrinter::PrintTemplateTypeParm(const TemplateTypeParmType *T,
std::string &S) {
if (!S.empty()) // Prefix the basic type, e.g. 'parmname X'.
S = ' ' + S;
if (!T->getName())
S = "type-parameter-" + llvm::utostr_32(T->getDepth()) + '-' +
llvm::utostr_32(T->getIndex()) + S;
else
S = T->getName()->getName().str() + S;
}
void TypePrinter::PrintSubstTemplateTypeParm(const SubstTemplateTypeParmType *T,
std::string &S) {
Print(T->getReplacementType(), S);
}
void TypePrinter::PrintTemplateSpecialization(
const TemplateSpecializationType *T,
std::string &S) {
std::string SpecString;
{
llvm::raw_string_ostream OS(SpecString);
T->getTemplateName().print(OS, Policy);
}
SpecString += TemplateSpecializationType::PrintTemplateArgumentList(
T->getArgs(),
T->getNumArgs(),
Policy);
if (S.empty())
S.swap(SpecString);
else
S = SpecString + ' ' + S;
}
void TypePrinter::PrintInjectedClassName(const InjectedClassNameType *T,
std::string &S) {
PrintTemplateSpecialization(T->getInjectedTST(), S);
}
void TypePrinter::PrintQualifiedName(const QualifiedNameType *T,
std::string &S) {
std::string MyString;
{
llvm::raw_string_ostream OS(MyString);
T->getQualifier()->print(OS, Policy);
}
std::string TypeStr;
PrintingPolicy InnerPolicy(Policy);
InnerPolicy.SuppressScope = true;
TypePrinter(InnerPolicy).Print(T->getNamedType(), TypeStr);
MyString += TypeStr;
if (S.empty())
S.swap(MyString);
else
S = MyString + ' ' + S;
}
void TypePrinter::PrintDependentName(const DependentNameType *T, std::string &S) {
std::string MyString;
{
llvm::raw_string_ostream OS(MyString);
switch (T->getKeyword()) {
case ETK_None: break;
case ETK_Typename: OS << "typename "; break;
case ETK_Class: OS << "class "; break;
case ETK_Struct: OS << "struct "; break;
case ETK_Union: OS << "union "; break;
case ETK_Enum: OS << "enum "; break;
}
T->getQualifier()->print(OS, Policy);
if (const IdentifierInfo *Ident = T->getIdentifier())
OS << Ident->getName();
else if (const TemplateSpecializationType *Spec = T->getTemplateId()) {
Spec->getTemplateName().print(OS, Policy, true);
OS << TemplateSpecializationType::PrintTemplateArgumentList(
Spec->getArgs(),
Spec->getNumArgs(),
Policy);
}
}
if (S.empty())
S.swap(MyString);
else
S = MyString + ' ' + S;
}
void TypePrinter::PrintObjCInterface(const ObjCInterfaceType *T,
std::string &S) {
if (!S.empty()) // Prefix the basic type, e.g. 'typedefname X'.
S = ' ' + S;
std::string ObjCQIString = T->getDecl()->getNameAsString();
if (T->getNumProtocols()) {
ObjCQIString += '<';
bool isFirst = true;
for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
E = T->qual_end();
I != E; ++I) {
if (isFirst)
isFirst = false;
else
ObjCQIString += ',';
ObjCQIString += (*I)->getNameAsString();
}
ObjCQIString += '>';
}
S = ObjCQIString + S;
}
void TypePrinter::PrintObjCObjectPointer(const ObjCObjectPointerType *T,
std::string &S) {
std::string ObjCQIString;
if (T->isObjCIdType() || T->isObjCQualifiedIdType())
ObjCQIString = "id";
else if (T->isObjCClassType() || T->isObjCQualifiedClassType())
ObjCQIString = "Class";
else if (T->isObjCSelType())
ObjCQIString = "SEL";
else
ObjCQIString = T->getInterfaceDecl()->getNameAsString();
if (!T->qual_empty()) {
ObjCQIString += '<';
for (ObjCObjectPointerType::qual_iterator I = T->qual_begin(),
E = T->qual_end();
I != E; ++I) {
ObjCQIString += (*I)->getNameAsString();
if (I+1 != E)
ObjCQIString += ',';
}
ObjCQIString += '>';
}
T->getPointeeType().getLocalQualifiers().getAsStringInternal(ObjCQIString,
Policy);
if (!T->isObjCIdType() && !T->isObjCQualifiedIdType())
ObjCQIString += " *"; // Don't forget the implicit pointer.
else if (!S.empty()) // Prefix the basic type, e.g. 'typedefname X'.
S = ' ' + S;
S = ObjCQIString + S;
}
static void PrintTemplateArgument(std::string &Buffer,
const TemplateArgument &Arg,
const PrintingPolicy &Policy) {
switch (Arg.getKind()) {
case TemplateArgument::Null:
assert(false && "Null template argument");
break;
case TemplateArgument::Type:
Arg.getAsType().getAsStringInternal(Buffer, Policy);
break;
case TemplateArgument::Declaration:
Buffer = cast<NamedDecl>(Arg.getAsDecl())->getNameAsString();
break;
case TemplateArgument::Template: {
llvm::raw_string_ostream s(Buffer);
Arg.getAsTemplate().print(s, Policy);
break;
}
case TemplateArgument::Integral:
Buffer = Arg.getAsIntegral()->toString(10, true);
break;
case TemplateArgument::Expression: {
llvm::raw_string_ostream s(Buffer);
Arg.getAsExpr()->printPretty(s, 0, Policy);
break;
}
case TemplateArgument::Pack:
assert(0 && "FIXME: Implement!");
break;
}
}
std::string TemplateSpecializationType::
PrintTemplateArgumentList(const TemplateArgumentListInfo &Args,
const PrintingPolicy &Policy) {
return PrintTemplateArgumentList(Args.getArgumentArray(),
Args.size(),
Policy);
}
std::string
TemplateSpecializationType::PrintTemplateArgumentList(
const TemplateArgument *Args,
unsigned NumArgs,
const PrintingPolicy &Policy) {
std::string SpecString;
SpecString += '<';
for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
if (Arg)
SpecString += ", ";
// Print the argument into a string.
std::string ArgString;
PrintTemplateArgument(ArgString, Args[Arg], Policy);
// If this is the first argument and its string representation
// begins with the global scope specifier ('::foo'), add a space
// to avoid printing the diagraph '<:'.
if (!Arg && !ArgString.empty() && ArgString[0] == ':')
SpecString += ' ';
SpecString += ArgString;
}
// If the last character of our string is '>', add another space to
// keep the two '>''s separate tokens. We don't *have* to do this in
// C++0x, but it's still good hygiene.
if (SpecString[SpecString.size() - 1] == '>')
SpecString += ' ';
SpecString += '>';
return SpecString;
}
// Sadly, repeat all that with TemplateArgLoc.
std::string TemplateSpecializationType::
PrintTemplateArgumentList(const TemplateArgumentLoc *Args, unsigned NumArgs,
const PrintingPolicy &Policy) {
std::string SpecString;
SpecString += '<';
for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
if (Arg)
SpecString += ", ";
// Print the argument into a string.
std::string ArgString;
PrintTemplateArgument(ArgString, Args[Arg].getArgument(), Policy);
// If this is the first argument and its string representation
// begins with the global scope specifier ('::foo'), add a space
// to avoid printing the diagraph '<:'.
if (!Arg && !ArgString.empty() && ArgString[0] == ':')
SpecString += ' ';
SpecString += ArgString;
}
// If the last character of our string is '>', add another space to
// keep the two '>''s separate tokens. We don't *have* to do this in
// C++0x, but it's still good hygiene.
if (SpecString[SpecString.size() - 1] == '>')
SpecString += ' ';
SpecString += '>';
return SpecString;
}
void QualType::dump(const char *msg) const {
std::string R = "identifier";
LangOptions LO;
getAsStringInternal(R, PrintingPolicy(LO));
if (msg)
llvm::errs() << msg << ": ";
llvm::errs() << R << "\n";
}
void QualType::dump() const {
dump("");
}
void Type::dump() const {
QualType(this, 0).dump();
}
std::string Qualifiers::getAsString() const {
LangOptions LO;
return getAsString(PrintingPolicy(LO));
}
// Appends qualifiers to the given string, separated by spaces. Will
// prefix a space if the string is non-empty. Will not append a final
// space.
void Qualifiers::getAsStringInternal(std::string &S,
const PrintingPolicy&) const {
AppendTypeQualList(S, getCVRQualifiers());
if (unsigned AddressSpace = getAddressSpace()) {
if (!S.empty()) S += ' ';
S += "__attribute__((address_space(";
S += llvm::utostr_32(AddressSpace);
S += ")))";
}
if (Qualifiers::GC GCAttrType = getObjCGCAttr()) {
if (!S.empty()) S += ' ';
S += "__attribute__((objc_gc(";
if (GCAttrType == Qualifiers::Weak)
S += "weak";
else
S += "strong";
S += ")))";
}
}
std::string QualType::getAsString() const {
std::string S;
LangOptions LO;
getAsStringInternal(S, PrintingPolicy(LO));
return S;
}
void QualType::getAsStringInternal(std::string &S,
const PrintingPolicy &Policy) const {
TypePrinter Printer(Policy);
Printer.Print(*this, S);
}
|