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