blob: 0c4896decf85ebfd401b5f295a52009fcd1ad60f [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
John McCallf82b4e82010-02-04 05:44:44 +0000285 switch(T->getCallConv()) {
286 case CC_Default:
287 default: break;
288 case CC_C:
289 S += " __attribute__((cdecl))";
290 break;
291 case CC_X86StdCall:
292 S += " __attribute__((stdcall))";
293 break;
294 case CC_X86FastCall:
295 S += " __attribute__((fastcall))";
296 break;
297 }
Douglas Gregor48026d22010-01-11 18:40:55 +0000298 if (T->getNoReturnAttr())
299 S += " __attribute__((noreturn))";
300
301
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000302 if (T->hasExceptionSpec()) {
303 S += " throw(";
304 if (T->hasAnyExceptionSpec())
305 S += "...";
306 else
307 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I) {
308 if (I)
309 S += ", ";
310
311 std::string ExceptionType;
312 Print(T->getExceptionType(I), ExceptionType);
313 S += ExceptionType;
314 }
315 S += ")";
316 }
317
Douglas Gregor48026d22010-01-11 18:40:55 +0000318 AppendTypeQualList(S, T->getTypeQuals());
Douglas Gregorfee8a3c2009-11-10 00:39:07 +0000319
Douglas Gregor48026d22010-01-11 18:40:55 +0000320 Print(T->getResultType(), S);
Douglas Gregorfee8a3c2009-11-10 00:39:07 +0000321}
322
323void TypePrinter::PrintFunctionNoProto(const FunctionNoProtoType *T,
324 std::string &S) {
325 // If needed for precedence reasons, wrap the inner part in grouping parens.
326 if (!S.empty())
327 S = "(" + S + ")";
328
329 S += "()";
330 if (T->getNoReturnAttr())
331 S += " __attribute__((noreturn))";
332 Print(T->getResultType(), S);
333}
334
John McCall3cb0ebd2010-03-10 03:28:59 +0000335static void PrintTypeSpec(const NamedDecl *D, std::string &S) {
336 IdentifierInfo *II = D->getIdentifier();
John McCalled976492009-12-04 22:46:56 +0000337 if (S.empty())
338 S = II->getName().str();
339 else
340 S = II->getName().str() + ' ' + S;
341}
342
John McCall3cb0ebd2010-03-10 03:28:59 +0000343void TypePrinter::PrintUnresolvedUsing(const UnresolvedUsingType *T,
344 std::string &S) {
345 PrintTypeSpec(T->getDecl(), S);
346}
347
Douglas Gregorfee8a3c2009-11-10 00:39:07 +0000348void TypePrinter::PrintTypedef(const TypedefType *T, std::string &S) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000349 PrintTypeSpec(T->getDecl(), S);
Douglas Gregorfee8a3c2009-11-10 00:39:07 +0000350}
351
352void TypePrinter::PrintTypeOfExpr(const TypeOfExprType *T, std::string &S) {
353 if (!S.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
354 S = ' ' + S;
355 std::string Str;
356 llvm::raw_string_ostream s(Str);
357 T->getUnderlyingExpr()->printPretty(s, 0, Policy);
358 S = "typeof " + s.str() + S;
359}
360
361void TypePrinter::PrintTypeOf(const TypeOfType *T, std::string &S) {
362 if (!S.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
363 S = ' ' + S;
364 std::string Tmp;
365 Print(T->getUnderlyingType(), Tmp);
366 S = "typeof(" + Tmp + ")" + S;
367}
368
369void TypePrinter::PrintDecltype(const DecltypeType *T, std::string &S) {
370 if (!S.empty()) // Prefix the basic type, e.g. 'decltype(t) X'.
371 S = ' ' + S;
372 std::string Str;
373 llvm::raw_string_ostream s(Str);
374 T->getUnderlyingExpr()->printPretty(s, 0, Policy);
375 S = "decltype(" + s.str() + ")" + S;
376}
377
John McCall7c2342d2010-03-10 11:27:22 +0000378/// Appends the given scope to the end of a string.
379void TypePrinter::AppendScope(DeclContext *DC, std::string &Buffer) {
380 if (DC->isTranslationUnit()) return;
381 AppendScope(DC->getParent(), Buffer);
382
383 unsigned OldSize = Buffer.size();
384
385 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
386 if (NS->getIdentifier())
387 Buffer += NS->getNameAsString();
388 else
389 Buffer += "<anonymous>";
390 } else if (ClassTemplateSpecializationDecl *Spec
391 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
392 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
393 std::string TemplateArgsStr
394 = TemplateSpecializationType::PrintTemplateArgumentList(
395 TemplateArgs.getFlatArgumentList(),
396 TemplateArgs.flat_size(),
397 Policy);
398 Buffer += Spec->getIdentifier()->getName();
399 Buffer += TemplateArgsStr;
400 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
401 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
402 Buffer += Typedef->getIdentifier()->getName();
403 else if (Tag->getIdentifier())
404 Buffer += Tag->getIdentifier()->getName();
405 }
406
407 if (Buffer.size() != OldSize)
408 Buffer += "::";
409}
410
John McCall3cb0ebd2010-03-10 03:28:59 +0000411void TypePrinter::PrintTag(TagDecl *D, std::string &InnerString) {
Douglas Gregorfee8a3c2009-11-10 00:39:07 +0000412 if (Policy.SuppressTag)
413 return;
John McCall7c2342d2010-03-10 11:27:22 +0000414
415 std::string Buffer;
John McCall73061d02010-03-19 07:56:44 +0000416 bool HasKindDecoration = false;
John McCall7c2342d2010-03-10 11:27:22 +0000417
418 // We don't print tags unless this is an elaborated type.
419 // In C, we just assume every RecordType is an elaborated type.
420 if (!Policy.LangOpts.CPlusPlus && !D->getTypedefForAnonDecl()) {
John McCall73061d02010-03-19 07:56:44 +0000421 HasKindDecoration = true;
John McCall7c2342d2010-03-10 11:27:22 +0000422 Buffer += D->getKindName();
423 Buffer += ' ';
424 }
425
426 if (!Policy.SuppressScope)
427 // Compute the full nested-name-specifier for this type. In C,
428 // this will always be empty.
429 AppendScope(D->getDeclContext(), Buffer);
430
John McCall3cb0ebd2010-03-10 03:28:59 +0000431 if (const IdentifierInfo *II = D->getIdentifier())
John McCall73061d02010-03-19 07:56:44 +0000432 Buffer += II->getNameStart();
John McCall3cb0ebd2010-03-10 03:28:59 +0000433 else if (TypedefDecl *Typedef = D->getTypedefForAnonDecl()) {
Douglas Gregorfee8a3c2009-11-10 00:39:07 +0000434 assert(Typedef->getIdentifier() && "Typedef without identifier?");
John McCall73061d02010-03-19 07:56:44 +0000435 Buffer += Typedef->getIdentifier()->getNameStart();
436 } else {
437 // Make an unambiguous representation for anonymous types, e.g.
438 // <anonymous enum at /usr/include/string.h:120:9>
439 llvm::raw_string_ostream OS(Buffer);
440 OS << "<anonymous";
441
442 // Suppress the redundant tag keyword if we just printed one.
443 // We don't have to worry about ElaboratedTypes here because you can't
444 // refer to an anonymous type with one.
445 if (!HasKindDecoration)
446 OS << " " << D->getKindName();
447
448 PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc(
449 D->getLocation());
450 OS << " at " << PLoc.getFilename()
451 << ':' << PLoc.getLine()
452 << ':' << PLoc.getColumn()
453 << '>';
454 OS.flush();
455 }
John McCall7c2342d2010-03-10 11:27:22 +0000456
Douglas Gregorfee8a3c2009-11-10 00:39:07 +0000457 // If this is a class template specialization, print the template
458 // arguments.
459 if (ClassTemplateSpecializationDecl *Spec
John McCall7c2342d2010-03-10 11:27:22 +0000460 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000461 const TemplateArgument *Args;
462 unsigned NumArgs;
463 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
464 const TemplateSpecializationType *TST =
465 cast<TemplateSpecializationType>(TAW->getType());
466 Args = TST->getArgs();
467 NumArgs = TST->getNumArgs();
468 } else {
469 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
470 Args = TemplateArgs.getFlatArgumentList();
471 NumArgs = TemplateArgs.flat_size();
472 }
John McCall7c2342d2010-03-10 11:27:22 +0000473 Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
474 NumArgs,
475 Policy);
Douglas Gregorfee8a3c2009-11-10 00:39:07 +0000476 }
John McCall7c2342d2010-03-10 11:27:22 +0000477
478 if (!InnerString.empty()) {
479 Buffer += ' ';
480 Buffer += InnerString;
481 }
482
483 std::swap(Buffer, InnerString);
Douglas Gregorfee8a3c2009-11-10 00:39:07 +0000484}
485
486void TypePrinter::PrintRecord(const RecordType *T, std::string &S) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000487 PrintTag(T->getDecl(), S);
Douglas Gregorfee8a3c2009-11-10 00:39:07 +0000488}
489
490void TypePrinter::PrintEnum(const EnumType *T, std::string &S) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000491 PrintTag(T->getDecl(), S);
Douglas Gregorfee8a3c2009-11-10 00:39:07 +0000492}
493
494void TypePrinter::PrintElaborated(const ElaboratedType *T, std::string &S) {
John McCall7c2342d2010-03-10 11:27:22 +0000495 Print(T->getUnderlyingType(), S);
John McCall327fb2d2010-03-10 21:05:46 +0000496
497 // We don't actually make these in C, but the language options
498 // sometimes lie to us -- for example, if someone calls
499 // QualType::getAsString(). Just suppress the redundant tag if so.
500 if (Policy.LangOpts.CPlusPlus)
501 S = std::string(T->getNameForTagKind(T->getTagKind())) + ' ' + S;
Douglas Gregorfee8a3c2009-11-10 00:39:07 +0000502}
503
504void TypePrinter::PrintTemplateTypeParm(const TemplateTypeParmType *T,
505 std::string &S) {
506 if (!S.empty()) // Prefix the basic type, e.g. 'parmname X'.
507 S = ' ' + S;
508
509 if (!T->getName())
510 S = "type-parameter-" + llvm::utostr_32(T->getDepth()) + '-' +
511 llvm::utostr_32(T->getIndex()) + S;
512 else
513 S = T->getName()->getName().str() + S;
514}
515
516void TypePrinter::PrintSubstTemplateTypeParm(const SubstTemplateTypeParmType *T,
517 std::string &S) {
518 Print(T->getReplacementType(), S);
519}
520
521void TypePrinter::PrintTemplateSpecialization(
522 const TemplateSpecializationType *T,
523 std::string &S) {
524 std::string SpecString;
525
526 {
527 llvm::raw_string_ostream OS(SpecString);
528 T->getTemplateName().print(OS, Policy);
529 }
530
531 SpecString += TemplateSpecializationType::PrintTemplateArgumentList(
532 T->getArgs(),
533 T->getNumArgs(),
534 Policy);
535 if (S.empty())
536 S.swap(SpecString);
537 else
538 S = SpecString + ' ' + S;
539}
540
John McCall3cb0ebd2010-03-10 03:28:59 +0000541void TypePrinter::PrintInjectedClassName(const InjectedClassNameType *T,
542 std::string &S) {
John McCall7c2342d2010-03-10 11:27:22 +0000543 PrintTemplateSpecialization(T->getUnderlyingTST(), S);
John McCall3cb0ebd2010-03-10 03:28:59 +0000544}
545
Douglas Gregorfee8a3c2009-11-10 00:39:07 +0000546void TypePrinter::PrintQualifiedName(const QualifiedNameType *T,
547 std::string &S) {
548 std::string MyString;
549
550 {
551 llvm::raw_string_ostream OS(MyString);
552 T->getQualifier()->print(OS, Policy);
553 }
554
555 std::string TypeStr;
556 PrintingPolicy InnerPolicy(Policy);
Douglas Gregorfee8a3c2009-11-10 00:39:07 +0000557 InnerPolicy.SuppressScope = true;
558 TypePrinter(InnerPolicy).Print(T->getNamedType(), TypeStr);
559
560 MyString += TypeStr;
561 if (S.empty())
562 S.swap(MyString);
563 else
564 S = MyString + ' ' + S;
565}
566
567void TypePrinter::PrintTypename(const TypenameType *T, std::string &S) {
568 std::string MyString;
569
570 {
571 llvm::raw_string_ostream OS(MyString);
572 OS << "typename ";
573 T->getQualifier()->print(OS, Policy);
574
575 if (const IdentifierInfo *Ident = T->getIdentifier())
576 OS << Ident->getName();
577 else if (const TemplateSpecializationType *Spec = T->getTemplateId()) {
578 Spec->getTemplateName().print(OS, Policy, true);
579 OS << TemplateSpecializationType::PrintTemplateArgumentList(
580 Spec->getArgs(),
581 Spec->getNumArgs(),
582 Policy);
583 }
584 }
585
586 if (S.empty())
587 S.swap(MyString);
588 else
589 S = MyString + ' ' + S;
590}
591
592void TypePrinter::PrintObjCInterface(const ObjCInterfaceType *T,
593 std::string &S) {
594 if (!S.empty()) // Prefix the basic type, e.g. 'typedefname X'.
595 S = ' ' + S;
596
597 std::string ObjCQIString = T->getDecl()->getNameAsString();
598 if (T->getNumProtocols()) {
599 ObjCQIString += '<';
600 bool isFirst = true;
601 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
602 E = T->qual_end();
603 I != E; ++I) {
604 if (isFirst)
605 isFirst = false;
606 else
607 ObjCQIString += ',';
608 ObjCQIString += (*I)->getNameAsString();
609 }
610 ObjCQIString += '>';
611 }
612 S = ObjCQIString + S;
613}
614
615void TypePrinter::PrintObjCObjectPointer(const ObjCObjectPointerType *T,
616 std::string &S) {
617 std::string ObjCQIString;
618
619 if (T->isObjCIdType() || T->isObjCQualifiedIdType())
620 ObjCQIString = "id";
621 else if (T->isObjCClassType() || T->isObjCQualifiedClassType())
622 ObjCQIString = "Class";
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000623 else if (T->isObjCSelType())
624 ObjCQIString = "SEL";
Douglas Gregorfee8a3c2009-11-10 00:39:07 +0000625 else
626 ObjCQIString = T->getInterfaceDecl()->getNameAsString();
627
628 if (!T->qual_empty()) {
629 ObjCQIString += '<';
630 for (ObjCObjectPointerType::qual_iterator I = T->qual_begin(),
631 E = T->qual_end();
632 I != E; ++I) {
633 ObjCQIString += (*I)->getNameAsString();
634 if (I+1 != E)
635 ObjCQIString += ',';
636 }
637 ObjCQIString += '>';
638 }
639
Douglas Gregora4923eb2009-11-16 21:35:15 +0000640 T->getPointeeType().getLocalQualifiers().getAsStringInternal(ObjCQIString,
641 Policy);
Douglas Gregorfee8a3c2009-11-10 00:39:07 +0000642
643 if (!T->isObjCIdType() && !T->isObjCQualifiedIdType())
644 ObjCQIString += " *"; // Don't forget the implicit pointer.
645 else if (!S.empty()) // Prefix the basic type, e.g. 'typedefname X'.
646 S = ' ' + S;
647
648 S = ObjCQIString + S;
649}
650
651static void PrintTemplateArgument(std::string &Buffer,
652 const TemplateArgument &Arg,
653 const PrintingPolicy &Policy) {
654 switch (Arg.getKind()) {
655 case TemplateArgument::Null:
656 assert(false && "Null template argument");
657 break;
658
659 case TemplateArgument::Type:
660 Arg.getAsType().getAsStringInternal(Buffer, Policy);
661 break;
662
663 case TemplateArgument::Declaration:
664 Buffer = cast<NamedDecl>(Arg.getAsDecl())->getNameAsString();
665 break;
666
Douglas Gregor788cd062009-11-11 01:00:40 +0000667 case TemplateArgument::Template: {
668 llvm::raw_string_ostream s(Buffer);
669 Arg.getAsTemplate().print(s, Policy);
Douglas Gregorfb898e12009-11-12 16:20:59 +0000670 break;
Douglas Gregor788cd062009-11-11 01:00:40 +0000671 }
672
Douglas Gregorfee8a3c2009-11-10 00:39:07 +0000673 case TemplateArgument::Integral:
674 Buffer = Arg.getAsIntegral()->toString(10, true);
675 break;
676
677 case TemplateArgument::Expression: {
678 llvm::raw_string_ostream s(Buffer);
679 Arg.getAsExpr()->printPretty(s, 0, Policy);
680 break;
681 }
682
683 case TemplateArgument::Pack:
684 assert(0 && "FIXME: Implement!");
685 break;
686 }
687}
688
John McCalld5532b62009-11-23 01:53:49 +0000689std::string TemplateSpecializationType::
690 PrintTemplateArgumentList(const TemplateArgumentListInfo &Args,
691 const PrintingPolicy &Policy) {
692 return PrintTemplateArgumentList(Args.getArgumentArray(),
693 Args.size(),
694 Policy);
695}
696
Douglas Gregorfee8a3c2009-11-10 00:39:07 +0000697std::string
698TemplateSpecializationType::PrintTemplateArgumentList(
699 const TemplateArgument *Args,
700 unsigned NumArgs,
701 const PrintingPolicy &Policy) {
702 std::string SpecString;
703 SpecString += '<';
704 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
705 if (Arg)
706 SpecString += ", ";
707
708 // Print the argument into a string.
709 std::string ArgString;
710 PrintTemplateArgument(ArgString, Args[Arg], Policy);
711
712 // If this is the first argument and its string representation
713 // begins with the global scope specifier ('::foo'), add a space
714 // to avoid printing the diagraph '<:'.
715 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
716 SpecString += ' ';
717
718 SpecString += ArgString;
719 }
720
721 // If the last character of our string is '>', add another space to
722 // keep the two '>''s separate tokens. We don't *have* to do this in
723 // C++0x, but it's still good hygiene.
724 if (SpecString[SpecString.size() - 1] == '>')
725 SpecString += ' ';
726
727 SpecString += '>';
728
729 return SpecString;
730}
731
732// Sadly, repeat all that with TemplateArgLoc.
733std::string TemplateSpecializationType::
734PrintTemplateArgumentList(const TemplateArgumentLoc *Args, unsigned NumArgs,
735 const PrintingPolicy &Policy) {
736 std::string SpecString;
737 SpecString += '<';
738 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
739 if (Arg)
740 SpecString += ", ";
741
742 // Print the argument into a string.
743 std::string ArgString;
744 PrintTemplateArgument(ArgString, Args[Arg].getArgument(), Policy);
745
746 // If this is the first argument and its string representation
747 // begins with the global scope specifier ('::foo'), add a space
748 // to avoid printing the diagraph '<:'.
749 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
750 SpecString += ' ';
751
752 SpecString += ArgString;
753 }
754
755 // If the last character of our string is '>', add another space to
756 // keep the two '>''s separate tokens. We don't *have* to do this in
757 // C++0x, but it's still good hygiene.
758 if (SpecString[SpecString.size() - 1] == '>')
759 SpecString += ' ';
760
761 SpecString += '>';
762
763 return SpecString;
764}
765
766void QualType::dump(const char *msg) const {
767 std::string R = "identifier";
768 LangOptions LO;
769 getAsStringInternal(R, PrintingPolicy(LO));
770 if (msg)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000771 llvm::errs() << msg << ": ";
772 llvm::errs() << R << "\n";
Douglas Gregorfee8a3c2009-11-10 00:39:07 +0000773}
774void QualType::dump() const {
775 dump("");
776}
777
778void Type::dump() const {
779 QualType(this, 0).dump();
780}
781
782std::string Qualifiers::getAsString() const {
783 LangOptions LO;
784 return getAsString(PrintingPolicy(LO));
785}
786
787// Appends qualifiers to the given string, separated by spaces. Will
788// prefix a space if the string is non-empty. Will not append a final
789// space.
790void Qualifiers::getAsStringInternal(std::string &S,
791 const PrintingPolicy&) const {
792 AppendTypeQualList(S, getCVRQualifiers());
793 if (unsigned AddressSpace = getAddressSpace()) {
794 if (!S.empty()) S += ' ';
795 S += "__attribute__((address_space(";
796 S += llvm::utostr_32(AddressSpace);
797 S += ")))";
798 }
799 if (Qualifiers::GC GCAttrType = getObjCGCAttr()) {
800 if (!S.empty()) S += ' ';
801 S += "__attribute__((objc_gc(";
802 if (GCAttrType == Qualifiers::Weak)
803 S += "weak";
804 else
805 S += "strong";
806 S += ")))";
807 }
808}
809
810std::string QualType::getAsString() const {
811 std::string S;
812 LangOptions LO;
813 getAsStringInternal(S, PrintingPolicy(LO));
814 return S;
815}
816
817void QualType::getAsStringInternal(std::string &S,
818 const PrintingPolicy &Policy) const {
819 TypePrinter Printer(Policy);
820 Printer.Print(*this, S);
821}
822