1 //===--- TypePrinter.cpp - Pretty-Print Clang Types -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This contains code to print types from Clang's type system.
12 //===----------------------------------------------------------------------===//
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"
21 #include "clang/Basic/SourceManager.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/Support/raw_ostream.h"
24 using namespace clang;
27 /// \brief RAII object that enables printing of the ARC __strong lifetime
29 class IncludeStrongLifetimeRAII {
30 PrintingPolicy &Policy;
34 explicit IncludeStrongLifetimeRAII(PrintingPolicy &Policy)
35 : Policy(Policy), Old(Policy.SuppressStrongLifetime) {
36 Policy.SuppressStrongLifetime = false;
39 ~IncludeStrongLifetimeRAII() {
40 Policy.SuppressStrongLifetime = Old;
45 PrintingPolicy Policy;
48 explicit TypePrinter(const PrintingPolicy &Policy) : Policy(Policy) { }
50 void print(const Type *ty, Qualifiers qs, std::string &buffer);
51 void print(QualType T, std::string &S);
52 void AppendScope(DeclContext *DC, std::string &S);
53 void printTag(TagDecl *T, std::string &S);
54 #define ABSTRACT_TYPE(CLASS, PARENT)
55 #define TYPE(CLASS, PARENT) \
56 void print##CLASS(const CLASS##Type *T, std::string &S);
57 #include "clang/AST/TypeNodes.def"
61 static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
62 if (TypeQuals & Qualifiers::Const) {
63 if (!S.empty()) S += ' ';
66 if (TypeQuals & Qualifiers::Volatile) {
67 if (!S.empty()) S += ' ';
70 if (TypeQuals & Qualifiers::Restrict) {
71 if (!S.empty()) S += ' ';
76 void TypePrinter::print(QualType t, std::string &buffer) {
77 SplitQualType split = t.split();
78 print(split.first, split.second, buffer);
81 void TypePrinter::print(const Type *T, Qualifiers Quals, std::string &buffer) {
83 buffer += "NULL TYPE";
87 if (Policy.SuppressSpecifiers && T->isSpecifierType())
90 // Print qualifiers as appropriate.
92 // CanPrefixQualifiers - We prefer to print type qualifiers before the type,
93 // so that we get "const int" instead of "int const", but we can't do this if
94 // the type is complex. For example if the type is "int*", we *must* print
95 // "int * const", printing "const int *" is different. Only do this when the
96 // type expands to a simple string.
97 bool CanPrefixQualifiers = false;
98 bool NeedARCStrongQualifier = false;
99 Type::TypeClass TC = T->getTypeClass();
100 if (const AutoType *AT = dyn_cast<AutoType>(T))
101 TC = AT->desugar()->getTypeClass();
102 if (const SubstTemplateTypeParmType *Subst
103 = dyn_cast<SubstTemplateTypeParmType>(T))
104 TC = Subst->getReplacementType()->getTypeClass();
109 case Type::UnresolvedUsing:
111 case Type::TypeOfExpr:
114 case Type::UnaryTransform:
117 case Type::Elaborated:
118 case Type::TemplateTypeParm:
119 case Type::SubstTemplateTypeParmPack:
120 case Type::TemplateSpecialization:
121 case Type::InjectedClassName:
122 case Type::DependentName:
123 case Type::DependentTemplateSpecialization:
124 case Type::ObjCObject:
125 case Type::ObjCInterface:
126 CanPrefixQualifiers = true;
129 case Type::ObjCObjectPointer:
130 CanPrefixQualifiers = T->isObjCIdType() || T->isObjCClassType() ||
131 T->isObjCQualifiedIdType() || T->isObjCQualifiedClassType();
134 case Type::ConstantArray:
135 case Type::IncompleteArray:
136 case Type::VariableArray:
137 case Type::DependentSizedArray:
138 NeedARCStrongQualifier = true;
142 case Type::BlockPointer:
143 case Type::LValueReference:
144 case Type::RValueReference:
145 case Type::MemberPointer:
146 case Type::DependentSizedExtVector:
148 case Type::ExtVector:
149 case Type::FunctionProto:
150 case Type::FunctionNoProto:
152 case Type::Attributed:
153 case Type::PackExpansion:
154 case Type::SubstTemplateTypeParm:
156 CanPrefixQualifiers = false;
160 if (!CanPrefixQualifiers && !Quals.empty()) {
161 std::string qualsBuffer;
162 if (NeedARCStrongQualifier) {
163 IncludeStrongLifetimeRAII Strong(Policy);
164 Quals.getAsStringInternal(qualsBuffer, Policy);
166 Quals.getAsStringInternal(qualsBuffer, Policy);
169 if (!qualsBuffer.empty()) {
170 if (!buffer.empty()) {
172 qualsBuffer += buffer;
174 std::swap(buffer, qualsBuffer);
178 switch (T->getTypeClass()) {
179 #define ABSTRACT_TYPE(CLASS, PARENT)
180 #define TYPE(CLASS, PARENT) case Type::CLASS: \
181 print##CLASS(cast<CLASS##Type>(T), buffer); \
183 #include "clang/AST/TypeNodes.def"
186 // If we're adding the qualifiers as a prefix, do it now.
187 if (CanPrefixQualifiers && !Quals.empty()) {
188 std::string qualsBuffer;
189 if (NeedARCStrongQualifier) {
190 IncludeStrongLifetimeRAII Strong(Policy);
191 Quals.getAsStringInternal(qualsBuffer, Policy);
193 Quals.getAsStringInternal(qualsBuffer, Policy);
196 if (!qualsBuffer.empty()) {
197 if (!buffer.empty()) {
199 qualsBuffer += buffer;
201 std::swap(buffer, qualsBuffer);
206 void TypePrinter::printBuiltin(const BuiltinType *T, std::string &S) {
208 S = T->getName(Policy.LangOpts);
210 // Prefix the basic type, e.g. 'int X'.
212 S = T->getName(Policy.LangOpts) + S;
216 void TypePrinter::printComplex(const ComplexType *T, std::string &S) {
217 print(T->getElementType(), S);
221 void TypePrinter::printPointer(const PointerType *T, std::string &S) {
224 // Handle things like 'int (*A)[4];' correctly.
225 // FIXME: this should include vectors, but vectors use attributes I guess.
226 if (isa<ArrayType>(T->getPointeeType()))
229 IncludeStrongLifetimeRAII Strong(Policy);
230 print(T->getPointeeType(), S);
233 void TypePrinter::printBlockPointer(const BlockPointerType *T, std::string &S) {
235 print(T->getPointeeType(), S);
238 void TypePrinter::printLValueReference(const LValueReferenceType *T,
242 // Handle things like 'int (&A)[4];' correctly.
243 // FIXME: this should include vectors, but vectors use attributes I guess.
244 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
247 IncludeStrongLifetimeRAII Strong(Policy);
248 print(T->getPointeeTypeAsWritten(), S);
251 void TypePrinter::printRValueReference(const RValueReferenceType *T,
255 // Handle things like 'int (&&A)[4];' correctly.
256 // FIXME: this should include vectors, but vectors use attributes I guess.
257 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
260 IncludeStrongLifetimeRAII Strong(Policy);
261 print(T->getPointeeTypeAsWritten(), S);
264 void TypePrinter::printMemberPointer(const MemberPointerType *T,
267 print(QualType(T->getClass(), 0), C);
271 // Handle things like 'int (Cls::*A)[4];' correctly.
272 // FIXME: this should include vectors, but vectors use attributes I guess.
273 if (isa<ArrayType>(T->getPointeeType()))
276 IncludeStrongLifetimeRAII Strong(Policy);
277 print(T->getPointeeType(), S);
280 void TypePrinter::printConstantArray(const ConstantArrayType *T,
283 S += llvm::utostr(T->getSize().getZExtValue());
286 IncludeStrongLifetimeRAII Strong(Policy);
287 print(T->getElementType(), S);
290 void TypePrinter::printIncompleteArray(const IncompleteArrayType *T,
293 IncludeStrongLifetimeRAII Strong(Policy);
294 print(T->getElementType(), S);
297 void TypePrinter::printVariableArray(const VariableArrayType *T,
301 if (T->getIndexTypeQualifiers().hasQualifiers()) {
302 AppendTypeQualList(S, T->getIndexTypeCVRQualifiers());
306 if (T->getSizeModifier() == VariableArrayType::Static)
308 else if (T->getSizeModifier() == VariableArrayType::Star)
311 if (T->getSizeExpr()) {
313 llvm::raw_string_ostream s(SStr);
314 T->getSizeExpr()->printPretty(s, 0, Policy);
319 IncludeStrongLifetimeRAII Strong(Policy);
320 print(T->getElementType(), S);
323 void TypePrinter::printDependentSizedArray(const DependentSizedArrayType *T,
327 if (T->getSizeExpr()) {
329 llvm::raw_string_ostream s(SStr);
330 T->getSizeExpr()->printPretty(s, 0, Policy);
335 IncludeStrongLifetimeRAII Strong(Policy);
336 print(T->getElementType(), S);
339 void TypePrinter::printDependentSizedExtVector(
340 const DependentSizedExtVectorType *T,
342 print(T->getElementType(), S);
344 S += " __attribute__((ext_vector_type(";
345 if (T->getSizeExpr()) {
347 llvm::raw_string_ostream s(SStr);
348 T->getSizeExpr()->printPretty(s, 0, Policy);
354 void TypePrinter::printVector(const VectorType *T, std::string &S) {
355 switch (T->getVectorKind()) {
356 case VectorType::AltiVecPixel:
357 S = "__vector __pixel " + S;
359 case VectorType::AltiVecBool:
360 print(T->getElementType(), S);
361 S = "__vector __bool " + S;
363 case VectorType::AltiVecVector:
364 print(T->getElementType(), S);
367 case VectorType::NeonVector:
368 print(T->getElementType(), S);
369 S = ("__attribute__((neon_vector_type(" +
370 llvm::utostr_32(T->getNumElements()) + "))) " + S);
372 case VectorType::NeonPolyVector:
373 print(T->getElementType(), S);
374 S = ("__attribute__((neon_polyvector_type(" +
375 llvm::utostr_32(T->getNumElements()) + "))) " + S);
377 case VectorType::GenericVector: {
378 // FIXME: We prefer to print the size directly here, but have no way
379 // to get the size of the type.
380 print(T->getElementType(), S);
381 std::string V = "__attribute__((__vector_size__(";
382 V += llvm::utostr_32(T->getNumElements()); // convert back to bytes.
384 print(T->getElementType(), ET);
385 V += " * sizeof(" + ET + ")))) ";
392 void TypePrinter::printExtVector(const ExtVectorType *T, std::string &S) {
393 S += " __attribute__((ext_vector_type(";
394 S += llvm::utostr_32(T->getNumElements());
396 print(T->getElementType(), S);
399 void TypePrinter::printFunctionProto(const FunctionProtoType *T,
401 // If needed for precedence reasons, wrap the inner part in grouping parens.
407 PrintingPolicy ParamPolicy(Policy);
408 ParamPolicy.SuppressSpecifiers = false;
409 for (unsigned i = 0, e = T->getNumArgs(); i != e; ++i) {
411 print(T->getArgType(i), Tmp);
416 if (T->isVariadic()) {
420 } else if (T->getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) {
421 // Do not emit int() if we have a proto, emit 'int(void)'.
427 FunctionType::ExtInfo Info = T->getExtInfo();
428 switch(Info.getCC()) {
432 S += " __attribute__((cdecl))";
435 S += " __attribute__((stdcall))";
438 S += " __attribute__((fastcall))";
441 S += " __attribute__((thiscall))";
444 S += " __attribute__((pascal))";
447 S += " __attribute__((pcs(\"aapcs\")))";
450 S += " __attribute__((pcs(\"aapcs-vfp\")))";
453 if (Info.getNoReturn())
454 S += " __attribute__((noreturn))";
455 if (Info.getRegParm())
456 S += " __attribute__((regparm (" +
457 llvm::utostr_32(Info.getRegParm()) + ")))";
459 AppendTypeQualList(S, T->getTypeQuals());
461 switch (T->getRefQualifier()) {
474 if (T->hasDynamicExceptionSpec()) {
476 if (T->getExceptionSpecType() == EST_MSAny)
479 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I) {
483 std::string ExceptionType;
484 print(T->getExceptionType(I), ExceptionType);
488 } else if (isNoexceptExceptionSpec(T->getExceptionSpecType())) {
490 if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
492 llvm::raw_string_ostream EOut(S);
493 T->getNoexceptExpr()->printPretty(EOut, 0, Policy);
500 print(T->getResultType(), S);
503 void TypePrinter::printFunctionNoProto(const FunctionNoProtoType *T,
505 // If needed for precedence reasons, wrap the inner part in grouping parens.
510 if (T->getNoReturnAttr())
511 S += " __attribute__((noreturn))";
512 print(T->getResultType(), S);
515 static void printTypeSpec(const NamedDecl *D, std::string &S) {
516 IdentifierInfo *II = D->getIdentifier();
518 S = II->getName().str();
520 S = II->getName().str() + ' ' + S;
523 void TypePrinter::printUnresolvedUsing(const UnresolvedUsingType *T,
525 printTypeSpec(T->getDecl(), S);
528 void TypePrinter::printTypedef(const TypedefType *T, std::string &S) {
529 printTypeSpec(T->getDecl(), S);
532 void TypePrinter::printTypeOfExpr(const TypeOfExprType *T, std::string &S) {
533 if (!S.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
536 llvm::raw_string_ostream s(Str);
537 T->getUnderlyingExpr()->printPretty(s, 0, Policy);
538 S = "typeof " + s.str() + S;
541 void TypePrinter::printTypeOf(const TypeOfType *T, std::string &S) {
542 if (!S.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
545 print(T->getUnderlyingType(), Tmp);
546 S = "typeof(" + Tmp + ")" + S;
549 void TypePrinter::printDecltype(const DecltypeType *T, std::string &S) {
550 if (!S.empty()) // Prefix the basic type, e.g. 'decltype(t) X'.
553 llvm::raw_string_ostream s(Str);
554 T->getUnderlyingExpr()->printPretty(s, 0, Policy);
555 S = "decltype(" + s.str() + ")" + S;
558 void TypePrinter::printUnaryTransform(const UnaryTransformType *T,
563 IncludeStrongLifetimeRAII Strong(Policy);
564 print(T->getBaseType(), Str);
566 switch (T->getUTTKind()) {
567 case UnaryTransformType::EnumUnderlyingType:
568 S = "__underlying_type(" + Str + ")" + S;
573 void TypePrinter::printAuto(const AutoType *T, std::string &S) {
574 // If the type has been deduced, do not print 'auto'.
575 if (T->isDeduced()) {
576 print(T->getDeducedType(), S);
578 if (!S.empty()) // Prefix the basic type, e.g. 'auto X'.
584 /// Appends the given scope to the end of a string.
585 void TypePrinter::AppendScope(DeclContext *DC, std::string &Buffer) {
586 if (DC->isTranslationUnit()) return;
587 AppendScope(DC->getParent(), Buffer);
589 unsigned OldSize = Buffer.size();
591 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
592 if (NS->getIdentifier())
593 Buffer += NS->getNameAsString();
595 Buffer += "<anonymous>";
596 } else if (ClassTemplateSpecializationDecl *Spec
597 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
598 IncludeStrongLifetimeRAII Strong(Policy);
599 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
600 std::string TemplateArgsStr
601 = TemplateSpecializationType::PrintTemplateArgumentList(
605 Buffer += Spec->getIdentifier()->getName();
606 Buffer += TemplateArgsStr;
607 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
608 if (TypedefNameDecl *Typedef = Tag->getTypedefNameForAnonDecl())
609 Buffer += Typedef->getIdentifier()->getName();
610 else if (Tag->getIdentifier())
611 Buffer += Tag->getIdentifier()->getName();
614 if (Buffer.size() != OldSize)
618 void TypePrinter::printTag(TagDecl *D, std::string &InnerString) {
619 if (Policy.SuppressTag)
623 bool HasKindDecoration = false;
625 // bool SuppressTagKeyword
626 // = Policy.LangOpts.CPlusPlus || Policy.SuppressTagKeyword;
628 // We don't print tags unless this is an elaborated type.
629 // In C, we just assume every RecordType is an elaborated type.
630 if (!(Policy.LangOpts.CPlusPlus || Policy.SuppressTagKeyword ||
631 D->getTypedefNameForAnonDecl())) {
632 HasKindDecoration = true;
633 Buffer += D->getKindName();
637 // Compute the full nested-name-specifier for this type.
638 // In C, this will always be empty except when the type
639 // being printed is anonymous within other Record.
640 if (!Policy.SuppressScope)
641 AppendScope(D->getDeclContext(), Buffer);
643 if (const IdentifierInfo *II = D->getIdentifier())
644 Buffer += II->getNameStart();
645 else if (TypedefNameDecl *Typedef = D->getTypedefNameForAnonDecl()) {
646 assert(Typedef->getIdentifier() && "Typedef without identifier?");
647 Buffer += Typedef->getIdentifier()->getNameStart();
649 // Make an unambiguous representation for anonymous types, e.g.
650 // <anonymous enum at /usr/include/string.h:120:9>
651 llvm::raw_string_ostream OS(Buffer);
654 if (Policy.AnonymousTagLocations) {
655 // Suppress the redundant tag keyword if we just printed one.
656 // We don't have to worry about ElaboratedTypes here because you can't
657 // refer to an anonymous type with one.
658 if (!HasKindDecoration)
659 OS << " " << D->getKindName();
661 PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc(
663 if (PLoc.isValid()) {
664 OS << " at " << PLoc.getFilename()
665 << ':' << PLoc.getLine()
666 << ':' << PLoc.getColumn();
673 // If this is a class template specialization, print the template
675 if (ClassTemplateSpecializationDecl *Spec
676 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
677 const TemplateArgument *Args;
679 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
680 const TemplateSpecializationType *TST =
681 cast<TemplateSpecializationType>(TAW->getType());
682 Args = TST->getArgs();
683 NumArgs = TST->getNumArgs();
685 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
686 Args = TemplateArgs.data();
687 NumArgs = TemplateArgs.size();
689 IncludeStrongLifetimeRAII Strong(Policy);
690 Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
695 if (!InnerString.empty()) {
697 Buffer += InnerString;
700 std::swap(Buffer, InnerString);
703 void TypePrinter::printRecord(const RecordType *T, std::string &S) {
704 printTag(T->getDecl(), S);
707 void TypePrinter::printEnum(const EnumType *T, std::string &S) {
708 printTag(T->getDecl(), S);
711 void TypePrinter::printTemplateTypeParm(const TemplateTypeParmType *T,
713 if (!S.empty()) // Prefix the basic type, e.g. 'parmname X'.
716 if (IdentifierInfo *Id = T->getIdentifier())
717 S = Id->getName().str() + S;
719 S = "type-parameter-" + llvm::utostr_32(T->getDepth()) + '-' +
720 llvm::utostr_32(T->getIndex()) + S;
723 void TypePrinter::printSubstTemplateTypeParm(const SubstTemplateTypeParmType *T,
725 IncludeStrongLifetimeRAII Strong(Policy);
726 print(T->getReplacementType(), S);
729 void TypePrinter::printSubstTemplateTypeParmPack(
730 const SubstTemplateTypeParmPackType *T,
732 IncludeStrongLifetimeRAII Strong(Policy);
733 printTemplateTypeParm(T->getReplacedParameter(), S);
736 void TypePrinter::printTemplateSpecialization(
737 const TemplateSpecializationType *T,
739 IncludeStrongLifetimeRAII Strong(Policy);
740 std::string SpecString;
743 llvm::raw_string_ostream OS(SpecString);
744 T->getTemplateName().print(OS, Policy);
747 SpecString += TemplateSpecializationType::PrintTemplateArgumentList(
754 S = SpecString + ' ' + S;
757 void TypePrinter::printInjectedClassName(const InjectedClassNameType *T,
759 printTemplateSpecialization(T->getInjectedTST(), S);
762 void TypePrinter::printElaborated(const ElaboratedType *T, std::string &S) {
763 std::string MyString;
766 llvm::raw_string_ostream OS(MyString);
767 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
768 if (T->getKeyword() != ETK_None)
770 NestedNameSpecifier* Qualifier = T->getQualifier();
772 Qualifier->print(OS, Policy);
776 PrintingPolicy InnerPolicy(Policy);
777 InnerPolicy.SuppressTagKeyword = true;
778 InnerPolicy.SuppressScope = true;
779 TypePrinter(InnerPolicy).print(T->getNamedType(), TypeStr);
785 S = MyString + ' ' + S;
788 void TypePrinter::printParen(const ParenType *T, std::string &S) {
789 if (!S.empty() && !isa<FunctionType>(T->getInnerType()))
791 print(T->getInnerType(), S);
794 void TypePrinter::printDependentName(const DependentNameType *T, std::string &S) {
795 std::string MyString;
798 llvm::raw_string_ostream OS(MyString);
799 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
800 if (T->getKeyword() != ETK_None)
803 T->getQualifier()->print(OS, Policy);
805 OS << T->getIdentifier()->getName();
811 S = MyString + ' ' + S;
814 void TypePrinter::printDependentTemplateSpecialization(
815 const DependentTemplateSpecializationType *T, std::string &S) {
816 IncludeStrongLifetimeRAII Strong(Policy);
817 std::string MyString;
819 llvm::raw_string_ostream OS(MyString);
821 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
822 if (T->getKeyword() != ETK_None)
825 if (T->getQualifier())
826 T->getQualifier()->print(OS, Policy);
827 OS << T->getIdentifier()->getName();
828 OS << TemplateSpecializationType::PrintTemplateArgumentList(
837 S = MyString + ' ' + S;
840 void TypePrinter::printPackExpansion(const PackExpansionType *T,
842 print(T->getPattern(), S);
846 void TypePrinter::printAttributed(const AttributedType *T,
848 // Prefer the macro forms of the GC and ownership qualifiers.
849 if (T->getAttrKind() == AttributedType::attr_objc_gc ||
850 T->getAttrKind() == AttributedType::attr_objc_ownership)
851 return print(T->getEquivalentType(), S);
853 print(T->getModifiedType(), S);
855 // TODO: not all attributes are GCC-style attributes.
856 S += " __attribute__((";
857 switch (T->getAttrKind()) {
858 case AttributedType::attr_address_space:
859 S += "address_space(";
860 S += T->getEquivalentType().getAddressSpace();
864 case AttributedType::attr_vector_size: {
865 S += "__vector_size__(";
866 if (const VectorType *vector =T->getEquivalentType()->getAs<VectorType>()) {
867 S += vector->getNumElements();
871 print(vector->getElementType(), tmp);
879 case AttributedType::attr_neon_vector_type:
880 case AttributedType::attr_neon_polyvector_type: {
881 if (T->getAttrKind() == AttributedType::attr_neon_vector_type)
882 S += "neon_vector_type(";
884 S += "neon_polyvector_type(";
885 const VectorType *vector = T->getEquivalentType()->getAs<VectorType>();
886 S += llvm::utostr_32(vector->getNumElements());
891 case AttributedType::attr_regparm: {
893 QualType t = T->getEquivalentType();
894 while (!t->isFunctionType())
895 t = t->getPointeeType();
896 S += t->getAs<FunctionType>()->getRegParmType();
901 case AttributedType::attr_objc_gc: {
904 QualType tmp = T->getEquivalentType();
905 while (tmp.getObjCGCAttr() == Qualifiers::GCNone) {
906 QualType next = tmp->getPointeeType();
907 if (next == tmp) break;
911 if (tmp.isObjCGCWeak())
919 case AttributedType::attr_objc_ownership:
920 S += "objc_ownership(";
921 switch (T->getEquivalentType().getObjCLifetime()) {
922 case Qualifiers::OCL_None: llvm_unreachable("no ownership!"); break;
923 case Qualifiers::OCL_ExplicitNone: S += "none"; break;
924 case Qualifiers::OCL_Strong: S += "strong"; break;
925 case Qualifiers::OCL_Weak: S += "weak"; break;
926 case Qualifiers::OCL_Autoreleasing: S += "autoreleasing"; break;
931 case AttributedType::attr_noreturn: S += "noreturn"; break;
932 case AttributedType::attr_cdecl: S += "cdecl"; break;
933 case AttributedType::attr_fastcall: S += "fastcall"; break;
934 case AttributedType::attr_stdcall: S += "stdcall"; break;
935 case AttributedType::attr_thiscall: S += "thiscall"; break;
936 case AttributedType::attr_pascal: S += "pascal"; break;
937 case AttributedType::attr_pcs: {
939 QualType t = T->getEquivalentType();
940 while (!t->isFunctionType())
941 t = t->getPointeeType();
942 S += (t->getAs<FunctionType>()->getCallConv() == CC_AAPCS ?
943 "\"aapcs\"" : "\"aapcs-vfp\"");
951 void TypePrinter::printObjCInterface(const ObjCInterfaceType *T,
953 if (!S.empty()) // Prefix the basic type, e.g. 'typedefname X'.
956 std::string ObjCQIString = T->getDecl()->getNameAsString();
957 S = ObjCQIString + S;
960 void TypePrinter::printObjCObject(const ObjCObjectType *T,
963 return print(T->getBaseType(), S);
966 print(T->getBaseType(), tmp);
969 for (ObjCObjectType::qual_iterator
970 I = T->qual_begin(), E = T->qual_end(); I != E; ++I) {
975 tmp += (*I)->getNameAsString();
986 void TypePrinter::printObjCObjectPointer(const ObjCObjectPointerType *T,
988 std::string ObjCQIString;
990 T->getPointeeType().getLocalQualifiers().getAsStringInternal(ObjCQIString,
992 if (!ObjCQIString.empty())
995 if (T->isObjCIdType() || T->isObjCQualifiedIdType())
996 ObjCQIString += "id";
997 else if (T->isObjCClassType() || T->isObjCQualifiedClassType())
998 ObjCQIString += "Class";
999 else if (T->isObjCSelType())
1000 ObjCQIString += "SEL";
1002 ObjCQIString += T->getInterfaceDecl()->getNameAsString();
1004 if (!T->qual_empty()) {
1005 ObjCQIString += '<';
1006 for (ObjCObjectPointerType::qual_iterator I = T->qual_begin(),
1009 ObjCQIString += (*I)->getNameAsString();
1011 ObjCQIString += ',';
1013 ObjCQIString += '>';
1016 if (!T->isObjCIdType() && !T->isObjCQualifiedIdType())
1017 ObjCQIString += " *"; // Don't forget the implicit pointer.
1018 else if (!S.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1021 S = ObjCQIString + S;
1024 std::string TemplateSpecializationType::
1025 PrintTemplateArgumentList(const TemplateArgumentListInfo &Args,
1026 const PrintingPolicy &Policy) {
1027 return PrintTemplateArgumentList(Args.getArgumentArray(),
1033 TemplateSpecializationType::PrintTemplateArgumentList(
1034 const TemplateArgument *Args,
1036 const PrintingPolicy &Policy,
1037 bool SkipBrackets) {
1038 std::string SpecString;
1042 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1043 if (SpecString.size() > unsigned(!SkipBrackets))
1046 // Print the argument into a string.
1047 std::string ArgString;
1048 if (Args[Arg].getKind() == TemplateArgument::Pack) {
1049 ArgString = PrintTemplateArgumentList(Args[Arg].pack_begin(),
1050 Args[Arg].pack_size(),
1053 llvm::raw_string_ostream ArgOut(ArgString);
1054 Args[Arg].print(Policy, ArgOut);
1057 // If this is the first argument and its string representation
1058 // begins with the global scope specifier ('::foo'), add a space
1059 // to avoid printing the diagraph '<:'.
1060 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1063 SpecString += ArgString;
1066 // If the last character of our string is '>', add another space to
1067 // keep the two '>''s separate tokens. We don't *have* to do this in
1068 // C++0x, but it's still good hygiene.
1069 if (!SpecString.empty() && SpecString[SpecString.size() - 1] == '>')
1078 // Sadly, repeat all that with TemplateArgLoc.
1079 std::string TemplateSpecializationType::
1080 PrintTemplateArgumentList(const TemplateArgumentLoc *Args, unsigned NumArgs,
1081 const PrintingPolicy &Policy) {
1082 std::string SpecString;
1084 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1085 if (SpecString.size() > 1)
1088 // Print the argument into a string.
1089 std::string ArgString;
1090 if (Args[Arg].getArgument().getKind() == TemplateArgument::Pack) {
1091 ArgString = PrintTemplateArgumentList(
1092 Args[Arg].getArgument().pack_begin(),
1093 Args[Arg].getArgument().pack_size(),
1096 llvm::raw_string_ostream ArgOut(ArgString);
1097 Args[Arg].getArgument().print(Policy, ArgOut);
1100 // If this is the first argument and its string representation
1101 // begins with the global scope specifier ('::foo'), add a space
1102 // to avoid printing the diagraph '<:'.
1103 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1106 SpecString += ArgString;
1109 // If the last character of our string is '>', add another space to
1110 // keep the two '>''s separate tokens. We don't *have* to do this in
1111 // C++0x, but it's still good hygiene.
1112 if (SpecString[SpecString.size() - 1] == '>')
1120 void QualType::dump(const char *msg) const {
1121 std::string R = "identifier";
1123 getAsStringInternal(R, PrintingPolicy(LO));
1125 llvm::errs() << msg << ": ";
1126 llvm::errs() << R << "\n";
1128 void QualType::dump() const {
1132 void Type::dump() const {
1133 QualType(this, 0).dump();
1136 std::string Qualifiers::getAsString() const {
1138 return getAsString(PrintingPolicy(LO));
1141 // Appends qualifiers to the given string, separated by spaces. Will
1142 // prefix a space if the string is non-empty. Will not append a final
1144 void Qualifiers::getAsStringInternal(std::string &S,
1145 const PrintingPolicy& Policy) const {
1146 AppendTypeQualList(S, getCVRQualifiers());
1147 if (unsigned addrspace = getAddressSpace()) {
1148 if (!S.empty()) S += ' ';
1149 S += "__attribute__((address_space(";
1150 S += llvm::utostr_32(addrspace);
1153 if (Qualifiers::GC gc = getObjCGCAttr()) {
1154 if (!S.empty()) S += ' ';
1155 if (gc == Qualifiers::Weak)
1160 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) {
1162 !(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime))
1166 case Qualifiers::OCL_None: llvm_unreachable("none but true");
1167 case Qualifiers::OCL_ExplicitNone: S += "__unsafe_unretained"; break;
1168 case Qualifiers::OCL_Strong:
1169 if (!Policy.SuppressStrongLifetime)
1173 case Qualifiers::OCL_Weak: S += "__weak"; break;
1174 case Qualifiers::OCL_Autoreleasing: S += "__autoreleasing"; break;
1179 std::string QualType::getAsString(const Type *ty, Qualifiers qs) {
1181 LangOptions options;
1182 getAsStringInternal(ty, qs, buffer, PrintingPolicy(options));
1186 void QualType::getAsStringInternal(const Type *ty, Qualifiers qs,
1187 std::string &buffer,
1188 const PrintingPolicy &policy) {
1189 TypePrinter(policy).print(ty, qs, buffer);