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:
127 CanPrefixQualifiers = true;
130 case Type::ObjCObjectPointer:
131 CanPrefixQualifiers = T->isObjCIdType() || T->isObjCClassType() ||
132 T->isObjCQualifiedIdType() || T->isObjCQualifiedClassType();
135 case Type::ConstantArray:
136 case Type::IncompleteArray:
137 case Type::VariableArray:
138 case Type::DependentSizedArray:
139 NeedARCStrongQualifier = true;
143 case Type::BlockPointer:
144 case Type::LValueReference:
145 case Type::RValueReference:
146 case Type::MemberPointer:
147 case Type::DependentSizedExtVector:
149 case Type::ExtVector:
150 case Type::FunctionProto:
151 case Type::FunctionNoProto:
153 case Type::Attributed:
154 case Type::PackExpansion:
155 case Type::SubstTemplateTypeParm:
157 CanPrefixQualifiers = false;
161 if (!CanPrefixQualifiers && !Quals.empty()) {
162 std::string qualsBuffer;
163 if (NeedARCStrongQualifier) {
164 IncludeStrongLifetimeRAII Strong(Policy);
165 Quals.getAsStringInternal(qualsBuffer, Policy);
167 Quals.getAsStringInternal(qualsBuffer, Policy);
170 if (!qualsBuffer.empty()) {
171 if (!buffer.empty()) {
173 qualsBuffer += buffer;
175 std::swap(buffer, qualsBuffer);
179 switch (T->getTypeClass()) {
180 #define ABSTRACT_TYPE(CLASS, PARENT)
181 #define TYPE(CLASS, PARENT) case Type::CLASS: \
182 print##CLASS(cast<CLASS##Type>(T), buffer); \
184 #include "clang/AST/TypeNodes.def"
187 // If we're adding the qualifiers as a prefix, do it now.
188 if (CanPrefixQualifiers && !Quals.empty()) {
189 std::string qualsBuffer;
190 if (NeedARCStrongQualifier) {
191 IncludeStrongLifetimeRAII Strong(Policy);
192 Quals.getAsStringInternal(qualsBuffer, Policy);
194 Quals.getAsStringInternal(qualsBuffer, Policy);
197 if (!qualsBuffer.empty()) {
198 if (!buffer.empty()) {
200 qualsBuffer += buffer;
202 std::swap(buffer, qualsBuffer);
207 void TypePrinter::printBuiltin(const BuiltinType *T, std::string &S) {
209 S = T->getName(Policy);
211 // Prefix the basic type, e.g. 'int X'.
213 S = T->getName(Policy) + S;
217 void TypePrinter::printComplex(const ComplexType *T, std::string &S) {
218 print(T->getElementType(), S);
222 void TypePrinter::printPointer(const PointerType *T, std::string &S) {
225 // Handle things like 'int (*A)[4];' correctly.
226 // FIXME: this should include vectors, but vectors use attributes I guess.
227 if (isa<ArrayType>(T->getPointeeType()))
230 IncludeStrongLifetimeRAII Strong(Policy);
231 print(T->getPointeeType(), S);
234 void TypePrinter::printBlockPointer(const BlockPointerType *T, std::string &S) {
236 print(T->getPointeeType(), S);
239 void TypePrinter::printLValueReference(const LValueReferenceType *T,
243 // Handle things like 'int (&A)[4];' correctly.
244 // FIXME: this should include vectors, but vectors use attributes I guess.
245 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
248 IncludeStrongLifetimeRAII Strong(Policy);
249 print(T->getPointeeTypeAsWritten(), S);
252 void TypePrinter::printRValueReference(const RValueReferenceType *T,
256 // Handle things like 'int (&&A)[4];' correctly.
257 // FIXME: this should include vectors, but vectors use attributes I guess.
258 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
261 IncludeStrongLifetimeRAII Strong(Policy);
262 print(T->getPointeeTypeAsWritten(), S);
265 void TypePrinter::printMemberPointer(const MemberPointerType *T,
268 print(QualType(T->getClass(), 0), C);
272 // Handle things like 'int (Cls::*A)[4];' correctly.
273 // FIXME: this should include vectors, but vectors use attributes I guess.
274 if (isa<ArrayType>(T->getPointeeType()))
277 IncludeStrongLifetimeRAII Strong(Policy);
278 print(T->getPointeeType(), S);
281 void TypePrinter::printConstantArray(const ConstantArrayType *T,
284 S += llvm::utostr(T->getSize().getZExtValue());
287 IncludeStrongLifetimeRAII Strong(Policy);
288 print(T->getElementType(), S);
291 void TypePrinter::printIncompleteArray(const IncompleteArrayType *T,
294 IncludeStrongLifetimeRAII Strong(Policy);
295 print(T->getElementType(), S);
298 void TypePrinter::printVariableArray(const VariableArrayType *T,
302 if (T->getIndexTypeQualifiers().hasQualifiers()) {
303 AppendTypeQualList(S, T->getIndexTypeCVRQualifiers());
307 if (T->getSizeModifier() == VariableArrayType::Static)
309 else if (T->getSizeModifier() == VariableArrayType::Star)
312 if (T->getSizeExpr()) {
314 llvm::raw_string_ostream s(SStr);
315 T->getSizeExpr()->printPretty(s, 0, Policy);
320 IncludeStrongLifetimeRAII Strong(Policy);
321 print(T->getElementType(), S);
324 void TypePrinter::printDependentSizedArray(const DependentSizedArrayType *T,
328 if (T->getSizeExpr()) {
330 llvm::raw_string_ostream s(SStr);
331 T->getSizeExpr()->printPretty(s, 0, Policy);
336 IncludeStrongLifetimeRAII Strong(Policy);
337 print(T->getElementType(), S);
340 void TypePrinter::printDependentSizedExtVector(
341 const DependentSizedExtVectorType *T,
343 print(T->getElementType(), S);
345 S += " __attribute__((ext_vector_type(";
346 if (T->getSizeExpr()) {
348 llvm::raw_string_ostream s(SStr);
349 T->getSizeExpr()->printPretty(s, 0, Policy);
355 void TypePrinter::printVector(const VectorType *T, std::string &S) {
356 switch (T->getVectorKind()) {
357 case VectorType::AltiVecPixel:
358 S = "__vector __pixel " + S;
360 case VectorType::AltiVecBool:
361 print(T->getElementType(), S);
362 S = "__vector __bool " + S;
364 case VectorType::AltiVecVector:
365 print(T->getElementType(), S);
368 case VectorType::NeonVector:
369 print(T->getElementType(), S);
370 S = ("__attribute__((neon_vector_type(" +
371 llvm::utostr_32(T->getNumElements()) + "))) " + S);
373 case VectorType::NeonPolyVector:
374 print(T->getElementType(), S);
375 S = ("__attribute__((neon_polyvector_type(" +
376 llvm::utostr_32(T->getNumElements()) + "))) " + S);
378 case VectorType::GenericVector: {
379 // FIXME: We prefer to print the size directly here, but have no way
380 // to get the size of the type.
381 print(T->getElementType(), S);
382 std::string V = "__attribute__((__vector_size__(";
383 V += llvm::utostr_32(T->getNumElements()); // convert back to bytes.
385 print(T->getElementType(), ET);
386 V += " * sizeof(" + ET + ")))) ";
393 void TypePrinter::printExtVector(const ExtVectorType *T, std::string &S) {
394 S += " __attribute__((ext_vector_type(";
395 S += llvm::utostr_32(T->getNumElements());
397 print(T->getElementType(), S);
400 void TypePrinter::printFunctionProto(const FunctionProtoType *T,
402 // If needed for precedence reasons, wrap the inner part in grouping parens.
408 PrintingPolicy ParamPolicy(Policy);
409 ParamPolicy.SuppressSpecifiers = false;
410 for (unsigned i = 0, e = T->getNumArgs(); i != e; ++i) {
412 print(T->getArgType(i), Tmp);
417 if (T->isVariadic()) {
421 } else if (T->getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) {
422 // Do not emit int() if we have a proto, emit 'int(void)'.
428 FunctionType::ExtInfo Info = T->getExtInfo();
429 switch(Info.getCC()) {
433 S += " __attribute__((cdecl))";
436 S += " __attribute__((stdcall))";
439 S += " __attribute__((fastcall))";
442 S += " __attribute__((thiscall))";
445 S += " __attribute__((pascal))";
448 S += " __attribute__((pcs(\"aapcs\")))";
451 S += " __attribute__((pcs(\"aapcs-vfp\")))";
454 if (Info.getNoReturn())
455 S += " __attribute__((noreturn))";
456 if (Info.getRegParm())
457 S += " __attribute__((regparm (" +
458 llvm::utostr_32(Info.getRegParm()) + ")))";
460 AppendTypeQualList(S, T->getTypeQuals());
462 switch (T->getRefQualifier()) {
475 if (T->hasDynamicExceptionSpec()) {
477 if (T->getExceptionSpecType() == EST_MSAny)
480 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I) {
484 std::string ExceptionType;
485 print(T->getExceptionType(I), ExceptionType);
489 } else if (isNoexceptExceptionSpec(T->getExceptionSpecType())) {
491 if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
493 llvm::raw_string_ostream EOut(S);
494 T->getNoexceptExpr()->printPretty(EOut, 0, Policy);
501 print(T->getResultType(), S);
504 void TypePrinter::printFunctionNoProto(const FunctionNoProtoType *T,
506 // If needed for precedence reasons, wrap the inner part in grouping parens.
511 if (T->getNoReturnAttr())
512 S += " __attribute__((noreturn))";
513 print(T->getResultType(), S);
516 static void printTypeSpec(const NamedDecl *D, std::string &S) {
517 IdentifierInfo *II = D->getIdentifier();
519 S = II->getName().str();
521 S = II->getName().str() + ' ' + S;
524 void TypePrinter::printUnresolvedUsing(const UnresolvedUsingType *T,
526 printTypeSpec(T->getDecl(), S);
529 void TypePrinter::printTypedef(const TypedefType *T, std::string &S) {
530 printTypeSpec(T->getDecl(), S);
533 void TypePrinter::printTypeOfExpr(const TypeOfExprType *T, std::string &S) {
534 if (!S.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
537 llvm::raw_string_ostream s(Str);
538 T->getUnderlyingExpr()->printPretty(s, 0, Policy);
539 S = "typeof " + s.str() + S;
542 void TypePrinter::printTypeOf(const TypeOfType *T, std::string &S) {
543 if (!S.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
546 print(T->getUnderlyingType(), Tmp);
547 S = "typeof(" + Tmp + ")" + S;
550 void TypePrinter::printDecltype(const DecltypeType *T, std::string &S) {
551 if (!S.empty()) // Prefix the basic type, e.g. 'decltype(t) X'.
554 llvm::raw_string_ostream s(Str);
555 T->getUnderlyingExpr()->printPretty(s, 0, Policy);
556 S = "decltype(" + s.str() + ")" + S;
559 void TypePrinter::printUnaryTransform(const UnaryTransformType *T,
564 IncludeStrongLifetimeRAII Strong(Policy);
565 print(T->getBaseType(), Str);
567 switch (T->getUTTKind()) {
568 case UnaryTransformType::EnumUnderlyingType:
569 S = "__underlying_type(" + Str + ")" + S;
574 void TypePrinter::printAuto(const AutoType *T, std::string &S) {
575 // If the type has been deduced, do not print 'auto'.
576 if (T->isDeduced()) {
577 print(T->getDeducedType(), S);
579 if (!S.empty()) // Prefix the basic type, e.g. 'auto X'.
585 void TypePrinter::printAtomic(const AtomicType *T, std::string &S) {
589 IncludeStrongLifetimeRAII Strong(Policy);
590 print(T->getValueType(), Str);
592 S = "_Atomic(" + Str + ")" + S;
595 /// Appends the given scope to the end of a string.
596 void TypePrinter::AppendScope(DeclContext *DC, std::string &Buffer) {
597 if (DC->isTranslationUnit()) return;
598 AppendScope(DC->getParent(), Buffer);
600 unsigned OldSize = Buffer.size();
602 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
603 if (NS->getIdentifier())
604 Buffer += NS->getNameAsString();
606 Buffer += "<anonymous>";
607 } else if (ClassTemplateSpecializationDecl *Spec
608 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
609 IncludeStrongLifetimeRAII Strong(Policy);
610 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
611 std::string TemplateArgsStr
612 = TemplateSpecializationType::PrintTemplateArgumentList(
616 Buffer += Spec->getIdentifier()->getName();
617 Buffer += TemplateArgsStr;
618 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
619 if (TypedefNameDecl *Typedef = Tag->getTypedefNameForAnonDecl())
620 Buffer += Typedef->getIdentifier()->getName();
621 else if (Tag->getIdentifier())
622 Buffer += Tag->getIdentifier()->getName();
625 if (Buffer.size() != OldSize)
629 void TypePrinter::printTag(TagDecl *D, std::string &InnerString) {
630 if (Policy.SuppressTag)
634 bool HasKindDecoration = false;
636 // bool SuppressTagKeyword
637 // = Policy.LangOpts.CPlusPlus || Policy.SuppressTagKeyword;
639 // We don't print tags unless this is an elaborated type.
640 // In C, we just assume every RecordType is an elaborated type.
641 if (!(Policy.LangOpts.CPlusPlus || Policy.SuppressTagKeyword ||
642 D->getTypedefNameForAnonDecl())) {
643 HasKindDecoration = true;
644 Buffer += D->getKindName();
648 // Compute the full nested-name-specifier for this type.
649 // In C, this will always be empty except when the type
650 // being printed is anonymous within other Record.
651 if (!Policy.SuppressScope)
652 AppendScope(D->getDeclContext(), Buffer);
654 if (const IdentifierInfo *II = D->getIdentifier())
655 Buffer += II->getNameStart();
656 else if (TypedefNameDecl *Typedef = D->getTypedefNameForAnonDecl()) {
657 assert(Typedef->getIdentifier() && "Typedef without identifier?");
658 Buffer += Typedef->getIdentifier()->getNameStart();
660 // Make an unambiguous representation for anonymous types, e.g.
661 // <anonymous enum at /usr/include/string.h:120:9>
662 llvm::raw_string_ostream OS(Buffer);
665 if (Policy.AnonymousTagLocations) {
666 // Suppress the redundant tag keyword if we just printed one.
667 // We don't have to worry about ElaboratedTypes here because you can't
668 // refer to an anonymous type with one.
669 if (!HasKindDecoration)
670 OS << " " << D->getKindName();
672 PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc(
674 if (PLoc.isValid()) {
675 OS << " at " << PLoc.getFilename()
676 << ':' << PLoc.getLine()
677 << ':' << PLoc.getColumn();
684 // If this is a class template specialization, print the template
686 if (ClassTemplateSpecializationDecl *Spec
687 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
688 const TemplateArgument *Args;
690 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
691 const TemplateSpecializationType *TST =
692 cast<TemplateSpecializationType>(TAW->getType());
693 Args = TST->getArgs();
694 NumArgs = TST->getNumArgs();
696 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
697 Args = TemplateArgs.data();
698 NumArgs = TemplateArgs.size();
700 IncludeStrongLifetimeRAII Strong(Policy);
701 Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
706 if (!InnerString.empty()) {
708 Buffer += InnerString;
711 std::swap(Buffer, InnerString);
714 void TypePrinter::printRecord(const RecordType *T, std::string &S) {
715 printTag(T->getDecl(), S);
718 void TypePrinter::printEnum(const EnumType *T, std::string &S) {
719 printTag(T->getDecl(), S);
722 void TypePrinter::printTemplateTypeParm(const TemplateTypeParmType *T,
724 if (!S.empty()) // Prefix the basic type, e.g. 'parmname X'.
727 if (IdentifierInfo *Id = T->getIdentifier())
728 S = Id->getName().str() + S;
730 S = "type-parameter-" + llvm::utostr_32(T->getDepth()) + '-' +
731 llvm::utostr_32(T->getIndex()) + S;
734 void TypePrinter::printSubstTemplateTypeParm(const SubstTemplateTypeParmType *T,
736 IncludeStrongLifetimeRAII Strong(Policy);
737 print(T->getReplacementType(), S);
740 void TypePrinter::printSubstTemplateTypeParmPack(
741 const SubstTemplateTypeParmPackType *T,
743 IncludeStrongLifetimeRAII Strong(Policy);
744 printTemplateTypeParm(T->getReplacedParameter(), S);
747 void TypePrinter::printTemplateSpecialization(
748 const TemplateSpecializationType *T,
750 IncludeStrongLifetimeRAII Strong(Policy);
751 std::string SpecString;
754 llvm::raw_string_ostream OS(SpecString);
755 T->getTemplateName().print(OS, Policy);
758 SpecString += TemplateSpecializationType::PrintTemplateArgumentList(
765 S = SpecString + ' ' + S;
768 void TypePrinter::printInjectedClassName(const InjectedClassNameType *T,
770 printTemplateSpecialization(T->getInjectedTST(), S);
773 void TypePrinter::printElaborated(const ElaboratedType *T, std::string &S) {
774 std::string MyString;
777 llvm::raw_string_ostream OS(MyString);
778 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
779 if (T->getKeyword() != ETK_None)
781 NestedNameSpecifier* Qualifier = T->getQualifier();
783 Qualifier->print(OS, Policy);
787 PrintingPolicy InnerPolicy(Policy);
788 InnerPolicy.SuppressTagKeyword = true;
789 InnerPolicy.SuppressScope = true;
790 TypePrinter(InnerPolicy).print(T->getNamedType(), TypeStr);
796 S = MyString + ' ' + S;
799 void TypePrinter::printParen(const ParenType *T, std::string &S) {
800 if (!S.empty() && !isa<FunctionType>(T->getInnerType()))
802 print(T->getInnerType(), S);
805 void TypePrinter::printDependentName(const DependentNameType *T, std::string &S) {
806 std::string MyString;
809 llvm::raw_string_ostream OS(MyString);
810 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
811 if (T->getKeyword() != ETK_None)
814 T->getQualifier()->print(OS, Policy);
816 OS << T->getIdentifier()->getName();
822 S = MyString + ' ' + S;
825 void TypePrinter::printDependentTemplateSpecialization(
826 const DependentTemplateSpecializationType *T, std::string &S) {
827 IncludeStrongLifetimeRAII Strong(Policy);
828 std::string MyString;
830 llvm::raw_string_ostream OS(MyString);
832 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
833 if (T->getKeyword() != ETK_None)
836 if (T->getQualifier())
837 T->getQualifier()->print(OS, Policy);
838 OS << T->getIdentifier()->getName();
839 OS << TemplateSpecializationType::PrintTemplateArgumentList(
848 S = MyString + ' ' + S;
851 void TypePrinter::printPackExpansion(const PackExpansionType *T,
853 print(T->getPattern(), S);
857 void TypePrinter::printAttributed(const AttributedType *T,
859 // Prefer the macro forms of the GC and ownership qualifiers.
860 if (T->getAttrKind() == AttributedType::attr_objc_gc ||
861 T->getAttrKind() == AttributedType::attr_objc_ownership)
862 return print(T->getEquivalentType(), S);
864 print(T->getModifiedType(), S);
866 // TODO: not all attributes are GCC-style attributes.
867 S += " __attribute__((";
868 switch (T->getAttrKind()) {
869 case AttributedType::attr_address_space:
870 S += "address_space(";
871 S += T->getEquivalentType().getAddressSpace();
875 case AttributedType::attr_vector_size: {
876 S += "__vector_size__(";
877 if (const VectorType *vector =T->getEquivalentType()->getAs<VectorType>()) {
878 S += vector->getNumElements();
882 print(vector->getElementType(), tmp);
890 case AttributedType::attr_neon_vector_type:
891 case AttributedType::attr_neon_polyvector_type: {
892 if (T->getAttrKind() == AttributedType::attr_neon_vector_type)
893 S += "neon_vector_type(";
895 S += "neon_polyvector_type(";
896 const VectorType *vector = T->getEquivalentType()->getAs<VectorType>();
897 S += llvm::utostr_32(vector->getNumElements());
902 case AttributedType::attr_regparm: {
904 QualType t = T->getEquivalentType();
905 while (!t->isFunctionType())
906 t = t->getPointeeType();
907 S += t->getAs<FunctionType>()->getRegParmType();
912 case AttributedType::attr_objc_gc: {
915 QualType tmp = T->getEquivalentType();
916 while (tmp.getObjCGCAttr() == Qualifiers::GCNone) {
917 QualType next = tmp->getPointeeType();
918 if (next == tmp) break;
922 if (tmp.isObjCGCWeak())
930 case AttributedType::attr_objc_ownership:
931 S += "objc_ownership(";
932 switch (T->getEquivalentType().getObjCLifetime()) {
933 case Qualifiers::OCL_None: llvm_unreachable("no ownership!"); break;
934 case Qualifiers::OCL_ExplicitNone: S += "none"; break;
935 case Qualifiers::OCL_Strong: S += "strong"; break;
936 case Qualifiers::OCL_Weak: S += "weak"; break;
937 case Qualifiers::OCL_Autoreleasing: S += "autoreleasing"; break;
942 case AttributedType::attr_noreturn: S += "noreturn"; break;
943 case AttributedType::attr_cdecl: S += "cdecl"; break;
944 case AttributedType::attr_fastcall: S += "fastcall"; break;
945 case AttributedType::attr_stdcall: S += "stdcall"; break;
946 case AttributedType::attr_thiscall: S += "thiscall"; break;
947 case AttributedType::attr_pascal: S += "pascal"; break;
948 case AttributedType::attr_pcs: {
950 QualType t = T->getEquivalentType();
951 while (!t->isFunctionType())
952 t = t->getPointeeType();
953 S += (t->getAs<FunctionType>()->getCallConv() == CC_AAPCS ?
954 "\"aapcs\"" : "\"aapcs-vfp\"");
962 void TypePrinter::printObjCInterface(const ObjCInterfaceType *T,
964 if (!S.empty()) // Prefix the basic type, e.g. 'typedefname X'.
967 std::string ObjCQIString = T->getDecl()->getNameAsString();
968 S = ObjCQIString + S;
971 void TypePrinter::printObjCObject(const ObjCObjectType *T,
974 return print(T->getBaseType(), S);
977 print(T->getBaseType(), tmp);
980 for (ObjCObjectType::qual_iterator
981 I = T->qual_begin(), E = T->qual_end(); I != E; ++I) {
986 tmp += (*I)->getNameAsString();
997 void TypePrinter::printObjCObjectPointer(const ObjCObjectPointerType *T,
999 std::string ObjCQIString;
1001 T->getPointeeType().getLocalQualifiers().getAsStringInternal(ObjCQIString,
1003 if (!ObjCQIString.empty())
1004 ObjCQIString += ' ';
1006 if (T->isObjCIdType() || T->isObjCQualifiedIdType())
1007 ObjCQIString += "id";
1008 else if (T->isObjCClassType() || T->isObjCQualifiedClassType())
1009 ObjCQIString += "Class";
1010 else if (T->isObjCSelType())
1011 ObjCQIString += "SEL";
1013 ObjCQIString += T->getInterfaceDecl()->getNameAsString();
1015 if (!T->qual_empty()) {
1016 ObjCQIString += '<';
1017 for (ObjCObjectPointerType::qual_iterator I = T->qual_begin(),
1020 ObjCQIString += (*I)->getNameAsString();
1022 ObjCQIString += ',';
1024 ObjCQIString += '>';
1027 if (!T->isObjCIdType() && !T->isObjCQualifiedIdType())
1028 ObjCQIString += " *"; // Don't forget the implicit pointer.
1029 else if (!S.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1032 S = ObjCQIString + S;
1035 std::string TemplateSpecializationType::
1036 PrintTemplateArgumentList(const TemplateArgumentListInfo &Args,
1037 const PrintingPolicy &Policy) {
1038 return PrintTemplateArgumentList(Args.getArgumentArray(),
1044 TemplateSpecializationType::PrintTemplateArgumentList(
1045 const TemplateArgument *Args,
1047 const PrintingPolicy &Policy,
1048 bool SkipBrackets) {
1049 std::string SpecString;
1053 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1054 if (SpecString.size() > unsigned(!SkipBrackets))
1057 // Print the argument into a string.
1058 std::string ArgString;
1059 if (Args[Arg].getKind() == TemplateArgument::Pack) {
1060 ArgString = PrintTemplateArgumentList(Args[Arg].pack_begin(),
1061 Args[Arg].pack_size(),
1064 llvm::raw_string_ostream ArgOut(ArgString);
1065 Args[Arg].print(Policy, ArgOut);
1068 // If this is the first argument and its string representation
1069 // begins with the global scope specifier ('::foo'), add a space
1070 // to avoid printing the diagraph '<:'.
1071 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1074 SpecString += ArgString;
1077 // If the last character of our string is '>', add another space to
1078 // keep the two '>''s separate tokens. We don't *have* to do this in
1079 // C++0x, but it's still good hygiene.
1080 if (!SpecString.empty() && SpecString[SpecString.size() - 1] == '>')
1089 // Sadly, repeat all that with TemplateArgLoc.
1090 std::string TemplateSpecializationType::
1091 PrintTemplateArgumentList(const TemplateArgumentLoc *Args, unsigned NumArgs,
1092 const PrintingPolicy &Policy) {
1093 std::string SpecString;
1095 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1096 if (SpecString.size() > 1)
1099 // Print the argument into a string.
1100 std::string ArgString;
1101 if (Args[Arg].getArgument().getKind() == TemplateArgument::Pack) {
1102 ArgString = PrintTemplateArgumentList(
1103 Args[Arg].getArgument().pack_begin(),
1104 Args[Arg].getArgument().pack_size(),
1107 llvm::raw_string_ostream ArgOut(ArgString);
1108 Args[Arg].getArgument().print(Policy, ArgOut);
1111 // If this is the first argument and its string representation
1112 // begins with the global scope specifier ('::foo'), add a space
1113 // to avoid printing the diagraph '<:'.
1114 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1117 SpecString += ArgString;
1120 // If the last character of our string is '>', add another space to
1121 // keep the two '>''s separate tokens. We don't *have* to do this in
1122 // C++0x, but it's still good hygiene.
1123 if (SpecString[SpecString.size() - 1] == '>')
1131 void QualType::dump(const char *msg) const {
1132 std::string R = "identifier";
1134 getAsStringInternal(R, PrintingPolicy(LO));
1136 llvm::errs() << msg << ": ";
1137 llvm::errs() << R << "\n";
1139 void QualType::dump() const {
1143 void Type::dump() const {
1144 QualType(this, 0).dump();
1147 std::string Qualifiers::getAsString() const {
1149 return getAsString(PrintingPolicy(LO));
1152 // Appends qualifiers to the given string, separated by spaces. Will
1153 // prefix a space if the string is non-empty. Will not append a final
1155 void Qualifiers::getAsStringInternal(std::string &S,
1156 const PrintingPolicy& Policy) const {
1157 AppendTypeQualList(S, getCVRQualifiers());
1158 if (unsigned addrspace = getAddressSpace()) {
1159 if (!S.empty()) S += ' ';
1160 S += "__attribute__((address_space(";
1161 S += llvm::utostr_32(addrspace);
1164 if (Qualifiers::GC gc = getObjCGCAttr()) {
1165 if (!S.empty()) S += ' ';
1166 if (gc == Qualifiers::Weak)
1171 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) {
1173 !(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime))
1177 case Qualifiers::OCL_None: llvm_unreachable("none but true");
1178 case Qualifiers::OCL_ExplicitNone: S += "__unsafe_unretained"; break;
1179 case Qualifiers::OCL_Strong:
1180 if (!Policy.SuppressStrongLifetime)
1184 case Qualifiers::OCL_Weak: S += "__weak"; break;
1185 case Qualifiers::OCL_Autoreleasing: S += "__autoreleasing"; break;
1190 std::string QualType::getAsString(const Type *ty, Qualifiers qs) {
1192 LangOptions options;
1193 getAsStringInternal(ty, qs, buffer, PrintingPolicy(options));
1197 void QualType::getAsStringInternal(const Type *ty, Qualifiers qs,
1198 std::string &buffer,
1199 const PrintingPolicy &policy) {
1200 TypePrinter(policy).print(ty, qs, buffer);