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