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.Ty, split.Quals, 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,
267 PrintingPolicy InnerPolicy(Policy);
268 Policy.SuppressTag = true;
269 std::string C = QualType(T->getClass(), 0).getAsString(InnerPolicy);
273 // Handle things like 'int (Cls::*A)[4];' correctly.
274 // FIXME: this should include vectors, but vectors use attributes I guess.
275 if (isa<ArrayType>(T->getPointeeType()))
278 IncludeStrongLifetimeRAII Strong(Policy);
279 print(T->getPointeeType(), S);
282 void TypePrinter::printConstantArray(const ConstantArrayType *T,
285 S += llvm::utostr(T->getSize().getZExtValue());
288 IncludeStrongLifetimeRAII Strong(Policy);
289 print(T->getElementType(), S);
292 void TypePrinter::printIncompleteArray(const IncompleteArrayType *T,
295 IncludeStrongLifetimeRAII Strong(Policy);
296 print(T->getElementType(), S);
299 void TypePrinter::printVariableArray(const VariableArrayType *T,
303 if (T->getIndexTypeQualifiers().hasQualifiers()) {
304 AppendTypeQualList(S, T->getIndexTypeCVRQualifiers());
308 if (T->getSizeModifier() == VariableArrayType::Static)
310 else if (T->getSizeModifier() == VariableArrayType::Star)
313 if (T->getSizeExpr()) {
315 llvm::raw_string_ostream s(SStr);
316 T->getSizeExpr()->printPretty(s, 0, Policy);
321 IncludeStrongLifetimeRAII Strong(Policy);
322 print(T->getElementType(), S);
325 void TypePrinter::printDependentSizedArray(const DependentSizedArrayType *T,
329 if (T->getSizeExpr()) {
331 llvm::raw_string_ostream s(SStr);
332 T->getSizeExpr()->printPretty(s, 0, Policy);
337 IncludeStrongLifetimeRAII Strong(Policy);
338 print(T->getElementType(), S);
341 void TypePrinter::printDependentSizedExtVector(
342 const DependentSizedExtVectorType *T,
344 print(T->getElementType(), S);
346 S += " __attribute__((ext_vector_type(";
347 if (T->getSizeExpr()) {
349 llvm::raw_string_ostream s(SStr);
350 T->getSizeExpr()->printPretty(s, 0, Policy);
356 void TypePrinter::printVector(const VectorType *T, std::string &S) {
357 switch (T->getVectorKind()) {
358 case VectorType::AltiVecPixel:
359 S = "__vector __pixel " + S;
361 case VectorType::AltiVecBool:
362 print(T->getElementType(), S);
363 S = "__vector __bool " + S;
365 case VectorType::AltiVecVector:
366 print(T->getElementType(), S);
369 case VectorType::NeonVector:
370 print(T->getElementType(), S);
371 S = ("__attribute__((neon_vector_type(" +
372 llvm::utostr_32(T->getNumElements()) + "))) " + S);
374 case VectorType::NeonPolyVector:
375 print(T->getElementType(), S);
376 S = ("__attribute__((neon_polyvector_type(" +
377 llvm::utostr_32(T->getNumElements()) + "))) " + S);
379 case VectorType::GenericVector: {
380 // FIXME: We prefer to print the size directly here, but have no way
381 // to get the size of the type.
382 print(T->getElementType(), S);
383 std::string V = "__attribute__((__vector_size__(";
384 V += llvm::utostr_32(T->getNumElements()); // convert back to bytes.
386 print(T->getElementType(), ET);
387 V += " * sizeof(" + ET + ")))) ";
394 void TypePrinter::printExtVector(const ExtVectorType *T, std::string &S) {
395 S += " __attribute__((ext_vector_type(";
396 S += llvm::utostr_32(T->getNumElements());
398 print(T->getElementType(), S);
402 FunctionProtoType::printExceptionSpecification(std::string &S,
403 PrintingPolicy Policy) const {
405 if (hasDynamicExceptionSpec()) {
407 if (getExceptionSpecType() == EST_MSAny)
410 for (unsigned I = 0, N = getNumExceptions(); I != N; ++I) {
414 S += getExceptionType(I).getAsString(Policy);
417 } else if (isNoexceptExceptionSpec(getExceptionSpecType())) {
419 if (getExceptionSpecType() == EST_ComputedNoexcept) {
421 llvm::raw_string_ostream EOut(S);
422 getNoexceptExpr()->printPretty(EOut, 0, Policy);
430 void TypePrinter::printFunctionProto(const FunctionProtoType *T,
432 // If needed for precedence reasons, wrap the inner part in grouping parens.
438 PrintingPolicy ParamPolicy(Policy);
439 ParamPolicy.SuppressSpecifiers = false;
440 for (unsigned i = 0, e = T->getNumArgs(); i != e; ++i) {
442 print(T->getArgType(i), Tmp);
447 if (T->isVariadic()) {
451 } else if (T->getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) {
452 // Do not emit int() if we have a proto, emit 'int(void)'.
458 FunctionType::ExtInfo Info = T->getExtInfo();
459 switch(Info.getCC()) {
460 case CC_Default: break;
462 S += " __attribute__((cdecl))";
465 S += " __attribute__((stdcall))";
468 S += " __attribute__((fastcall))";
471 S += " __attribute__((thiscall))";
474 S += " __attribute__((pascal))";
477 S += " __attribute__((pcs(\"aapcs\")))";
480 S += " __attribute__((pcs(\"aapcs-vfp\")))";
483 if (Info.getNoReturn())
484 S += " __attribute__((noreturn))";
485 if (Info.getRegParm())
486 S += " __attribute__((regparm (" +
487 llvm::utostr_32(Info.getRegParm()) + ")))";
489 AppendTypeQualList(S, T->getTypeQuals());
491 switch (T->getRefQualifier()) {
503 T->printExceptionSpecification(S, Policy);
504 if (T->hasTrailingReturn()) {
506 print(T->getResultType(), ResultS);
507 S = "auto " + S + " -> " + ResultS;
509 print(T->getResultType(), S);
512 void TypePrinter::printFunctionNoProto(const FunctionNoProtoType *T,
514 // If needed for precedence reasons, wrap the inner part in grouping parens.
519 if (T->getNoReturnAttr())
520 S += " __attribute__((noreturn))";
521 print(T->getResultType(), S);
524 static void printTypeSpec(const NamedDecl *D, std::string &S) {
525 IdentifierInfo *II = D->getIdentifier();
527 S = II->getName().str();
529 S = II->getName().str() + ' ' + S;
532 void TypePrinter::printUnresolvedUsing(const UnresolvedUsingType *T,
534 printTypeSpec(T->getDecl(), S);
537 void TypePrinter::printTypedef(const TypedefType *T, std::string &S) {
538 printTypeSpec(T->getDecl(), S);
541 void TypePrinter::printTypeOfExpr(const TypeOfExprType *T, std::string &S) {
542 if (!S.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
545 llvm::raw_string_ostream s(Str);
546 T->getUnderlyingExpr()->printPretty(s, 0, Policy);
547 S = "typeof " + s.str() + S;
550 void TypePrinter::printTypeOf(const TypeOfType *T, std::string &S) {
551 if (!S.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
554 print(T->getUnderlyingType(), Tmp);
555 S = "typeof(" + Tmp + ")" + S;
558 void TypePrinter::printDecltype(const DecltypeType *T, std::string &S) {
559 if (!S.empty()) // Prefix the basic type, e.g. 'decltype(t) X'.
562 llvm::raw_string_ostream s(Str);
563 T->getUnderlyingExpr()->printPretty(s, 0, Policy);
564 S = "decltype(" + s.str() + ")" + S;
567 void TypePrinter::printUnaryTransform(const UnaryTransformType *T,
572 IncludeStrongLifetimeRAII Strong(Policy);
573 print(T->getBaseType(), Str);
575 switch (T->getUTTKind()) {
576 case UnaryTransformType::EnumUnderlyingType:
577 S = "__underlying_type(" + Str + ")" + S;
582 void TypePrinter::printAuto(const AutoType *T, std::string &S) {
583 // If the type has been deduced, do not print 'auto'.
584 if (T->isDeduced()) {
585 print(T->getDeducedType(), S);
587 if (!S.empty()) // Prefix the basic type, e.g. 'auto X'.
593 void TypePrinter::printAtomic(const AtomicType *T, std::string &S) {
597 IncludeStrongLifetimeRAII Strong(Policy);
598 print(T->getValueType(), Str);
600 S = "_Atomic(" + Str + ")" + S;
603 /// Appends the given scope to the end of a string.
604 void TypePrinter::AppendScope(DeclContext *DC, std::string &Buffer) {
605 if (DC->isTranslationUnit()) return;
606 AppendScope(DC->getParent(), Buffer);
608 unsigned OldSize = Buffer.size();
610 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
611 if (Policy.SuppressUnwrittenScope &&
612 (NS->isAnonymousNamespace() || NS->isInline()))
614 if (NS->getIdentifier())
615 Buffer += NS->getNameAsString();
617 Buffer += "<anonymous>";
618 } else if (ClassTemplateSpecializationDecl *Spec
619 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
620 IncludeStrongLifetimeRAII Strong(Policy);
621 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
622 std::string TemplateArgsStr
623 = TemplateSpecializationType::PrintTemplateArgumentList(
627 Buffer += Spec->getIdentifier()->getName();
628 Buffer += TemplateArgsStr;
629 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
630 if (TypedefNameDecl *Typedef = Tag->getTypedefNameForAnonDecl())
631 Buffer += Typedef->getIdentifier()->getName();
632 else if (Tag->getIdentifier())
633 Buffer += Tag->getIdentifier()->getName();
638 if (Buffer.size() != OldSize)
642 void TypePrinter::printTag(TagDecl *D, std::string &InnerString) {
643 if (Policy.SuppressTag)
647 bool HasKindDecoration = false;
649 // bool SuppressTagKeyword
650 // = Policy.LangOpts.CPlusPlus || Policy.SuppressTagKeyword;
652 // We don't print tags unless this is an elaborated type.
653 // In C, we just assume every RecordType is an elaborated type.
654 if (!(Policy.LangOpts.CPlusPlus || Policy.SuppressTagKeyword ||
655 D->getTypedefNameForAnonDecl())) {
656 HasKindDecoration = true;
657 Buffer += D->getKindName();
661 // Compute the full nested-name-specifier for this type.
662 // In C, this will always be empty except when the type
663 // being printed is anonymous within other Record.
664 if (!Policy.SuppressScope)
665 AppendScope(D->getDeclContext(), Buffer);
667 if (const IdentifierInfo *II = D->getIdentifier())
668 Buffer += II->getNameStart();
669 else if (TypedefNameDecl *Typedef = D->getTypedefNameForAnonDecl()) {
670 assert(Typedef->getIdentifier() && "Typedef without identifier?");
671 Buffer += Typedef->getIdentifier()->getNameStart();
673 // Make an unambiguous representation for anonymous types, e.g.
674 // <anonymous enum at /usr/include/string.h:120:9>
675 llvm::raw_string_ostream OS(Buffer);
677 if (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda()) {
679 HasKindDecoration = true;
684 if (Policy.AnonymousTagLocations) {
685 // Suppress the redundant tag keyword if we just printed one.
686 // We don't have to worry about ElaboratedTypes here because you can't
687 // refer to an anonymous type with one.
688 if (!HasKindDecoration)
689 OS << " " << D->getKindName();
691 PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc(
693 if (PLoc.isValid()) {
694 OS << " at " << PLoc.getFilename()
695 << ':' << PLoc.getLine()
696 << ':' << PLoc.getColumn();
703 // If this is a class template specialization, print the template
705 if (ClassTemplateSpecializationDecl *Spec
706 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
707 const TemplateArgument *Args;
709 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
710 const TemplateSpecializationType *TST =
711 cast<TemplateSpecializationType>(TAW->getType());
712 Args = TST->getArgs();
713 NumArgs = TST->getNumArgs();
715 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
716 Args = TemplateArgs.data();
717 NumArgs = TemplateArgs.size();
719 IncludeStrongLifetimeRAII Strong(Policy);
720 Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
725 if (!InnerString.empty()) {
727 Buffer += InnerString;
730 std::swap(Buffer, InnerString);
733 void TypePrinter::printRecord(const RecordType *T, std::string &S) {
734 printTag(T->getDecl(), S);
737 void TypePrinter::printEnum(const EnumType *T, std::string &S) {
738 printTag(T->getDecl(), S);
741 void TypePrinter::printTemplateTypeParm(const TemplateTypeParmType *T,
743 if (!S.empty()) // Prefix the basic type, e.g. 'parmname X'.
746 if (IdentifierInfo *Id = T->getIdentifier())
747 S = Id->getName().str() + S;
749 S = "type-parameter-" + llvm::utostr_32(T->getDepth()) + '-' +
750 llvm::utostr_32(T->getIndex()) + S;
753 void TypePrinter::printSubstTemplateTypeParm(const SubstTemplateTypeParmType *T,
755 IncludeStrongLifetimeRAII Strong(Policy);
756 print(T->getReplacementType(), S);
759 void TypePrinter::printSubstTemplateTypeParmPack(
760 const SubstTemplateTypeParmPackType *T,
762 IncludeStrongLifetimeRAII Strong(Policy);
763 printTemplateTypeParm(T->getReplacedParameter(), S);
766 void TypePrinter::printTemplateSpecialization(
767 const TemplateSpecializationType *T,
769 IncludeStrongLifetimeRAII Strong(Policy);
770 std::string SpecString;
773 llvm::raw_string_ostream OS(SpecString);
774 T->getTemplateName().print(OS, Policy);
777 SpecString += TemplateSpecializationType::PrintTemplateArgumentList(
784 S = SpecString + ' ' + S;
787 void TypePrinter::printInjectedClassName(const InjectedClassNameType *T,
789 printTemplateSpecialization(T->getInjectedTST(), S);
792 void TypePrinter::printElaborated(const ElaboratedType *T, std::string &S) {
793 std::string MyString;
796 llvm::raw_string_ostream OS(MyString);
797 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
798 if (T->getKeyword() != ETK_None)
800 NestedNameSpecifier* Qualifier = T->getQualifier();
802 Qualifier->print(OS, Policy);
806 PrintingPolicy InnerPolicy(Policy);
807 InnerPolicy.SuppressTagKeyword = true;
808 InnerPolicy.SuppressScope = true;
809 TypePrinter(InnerPolicy).print(T->getNamedType(), TypeStr);
815 S = MyString + ' ' + S;
818 void TypePrinter::printParen(const ParenType *T, std::string &S) {
819 if (!S.empty() && !isa<FunctionType>(T->getInnerType()))
821 print(T->getInnerType(), S);
824 void TypePrinter::printDependentName(const DependentNameType *T, std::string &S) {
825 std::string MyString;
828 llvm::raw_string_ostream OS(MyString);
829 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
830 if (T->getKeyword() != ETK_None)
833 T->getQualifier()->print(OS, Policy);
835 OS << T->getIdentifier()->getName();
841 S = MyString + ' ' + S;
844 void TypePrinter::printDependentTemplateSpecialization(
845 const DependentTemplateSpecializationType *T, std::string &S) {
846 IncludeStrongLifetimeRAII Strong(Policy);
847 std::string MyString;
849 llvm::raw_string_ostream OS(MyString);
851 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
852 if (T->getKeyword() != ETK_None)
855 if (T->getQualifier())
856 T->getQualifier()->print(OS, Policy);
857 OS << T->getIdentifier()->getName();
858 OS << TemplateSpecializationType::PrintTemplateArgumentList(
867 S = MyString + ' ' + S;
870 void TypePrinter::printPackExpansion(const PackExpansionType *T,
872 print(T->getPattern(), S);
876 void TypePrinter::printAttributed(const AttributedType *T,
878 // Prefer the macro forms of the GC and ownership qualifiers.
879 if (T->getAttrKind() == AttributedType::attr_objc_gc ||
880 T->getAttrKind() == AttributedType::attr_objc_ownership)
881 return print(T->getEquivalentType(), S);
883 print(T->getModifiedType(), S);
885 // TODO: not all attributes are GCC-style attributes.
886 S += " __attribute__((";
887 switch (T->getAttrKind()) {
888 case AttributedType::attr_address_space:
889 S += "address_space(";
890 S += T->getEquivalentType().getAddressSpace();
894 case AttributedType::attr_vector_size: {
895 S += "__vector_size__(";
896 if (const VectorType *vector =T->getEquivalentType()->getAs<VectorType>()) {
897 S += vector->getNumElements();
901 print(vector->getElementType(), tmp);
909 case AttributedType::attr_neon_vector_type:
910 case AttributedType::attr_neon_polyvector_type: {
911 if (T->getAttrKind() == AttributedType::attr_neon_vector_type)
912 S += "neon_vector_type(";
914 S += "neon_polyvector_type(";
915 const VectorType *vector = T->getEquivalentType()->getAs<VectorType>();
916 S += llvm::utostr_32(vector->getNumElements());
921 case AttributedType::attr_regparm: {
923 QualType t = T->getEquivalentType();
924 while (!t->isFunctionType())
925 t = t->getPointeeType();
926 S += t->getAs<FunctionType>()->getRegParmType();
931 case AttributedType::attr_objc_gc: {
934 QualType tmp = T->getEquivalentType();
935 while (tmp.getObjCGCAttr() == Qualifiers::GCNone) {
936 QualType next = tmp->getPointeeType();
937 if (next == tmp) break;
941 if (tmp.isObjCGCWeak())
949 case AttributedType::attr_objc_ownership:
950 S += "objc_ownership(";
951 switch (T->getEquivalentType().getObjCLifetime()) {
952 case Qualifiers::OCL_None: llvm_unreachable("no ownership!");
953 case Qualifiers::OCL_ExplicitNone: S += "none"; break;
954 case Qualifiers::OCL_Strong: S += "strong"; break;
955 case Qualifiers::OCL_Weak: S += "weak"; break;
956 case Qualifiers::OCL_Autoreleasing: S += "autoreleasing"; break;
961 case AttributedType::attr_noreturn: S += "noreturn"; break;
962 case AttributedType::attr_cdecl: S += "cdecl"; break;
963 case AttributedType::attr_fastcall: S += "fastcall"; break;
964 case AttributedType::attr_stdcall: S += "stdcall"; break;
965 case AttributedType::attr_thiscall: S += "thiscall"; break;
966 case AttributedType::attr_pascal: S += "pascal"; break;
967 case AttributedType::attr_pcs: {
969 QualType t = T->getEquivalentType();
970 while (!t->isFunctionType())
971 t = t->getPointeeType();
972 S += (t->getAs<FunctionType>()->getCallConv() == CC_AAPCS ?
973 "\"aapcs\"" : "\"aapcs-vfp\"");
981 void TypePrinter::printObjCInterface(const ObjCInterfaceType *T,
983 if (!S.empty()) // Prefix the basic type, e.g. 'typedefname X'.
986 std::string ObjCQIString = T->getDecl()->getNameAsString();
987 S = ObjCQIString + S;
990 void TypePrinter::printObjCObject(const ObjCObjectType *T,
993 return print(T->getBaseType(), S);
996 print(T->getBaseType(), tmp);
999 for (ObjCObjectType::qual_iterator
1000 I = T->qual_begin(), E = T->qual_end(); I != E; ++I) {
1005 tmp += (*I)->getNameAsString();
1016 void TypePrinter::printObjCObjectPointer(const ObjCObjectPointerType *T,
1018 std::string ObjCQIString;
1020 T->getPointeeType().getLocalQualifiers().getAsStringInternal(ObjCQIString,
1022 if (!ObjCQIString.empty())
1023 ObjCQIString += ' ';
1025 if (T->isObjCIdType() || T->isObjCQualifiedIdType())
1026 ObjCQIString += "id";
1027 else if (T->isObjCClassType() || T->isObjCQualifiedClassType())
1028 ObjCQIString += "Class";
1029 else if (T->isObjCSelType())
1030 ObjCQIString += "SEL";
1032 ObjCQIString += T->getInterfaceDecl()->getNameAsString();
1034 if (!T->qual_empty()) {
1035 ObjCQIString += '<';
1036 for (ObjCObjectPointerType::qual_iterator I = T->qual_begin(),
1039 ObjCQIString += (*I)->getNameAsString();
1041 ObjCQIString += ',';
1043 ObjCQIString += '>';
1046 if (!T->isObjCIdType() && !T->isObjCQualifiedIdType())
1047 ObjCQIString += " *"; // Don't forget the implicit pointer.
1048 else if (!S.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1051 S = ObjCQIString + S;
1054 std::string TemplateSpecializationType::
1055 PrintTemplateArgumentList(const TemplateArgumentListInfo &Args,
1056 const PrintingPolicy &Policy) {
1057 return PrintTemplateArgumentList(Args.getArgumentArray(),
1063 TemplateSpecializationType::PrintTemplateArgumentList(
1064 const TemplateArgument *Args,
1066 const PrintingPolicy &Policy,
1067 bool SkipBrackets) {
1068 std::string SpecString;
1072 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1073 if (SpecString.size() > unsigned(!SkipBrackets))
1076 // Print the argument into a string.
1077 std::string ArgString;
1078 if (Args[Arg].getKind() == TemplateArgument::Pack) {
1079 ArgString = PrintTemplateArgumentList(Args[Arg].pack_begin(),
1080 Args[Arg].pack_size(),
1083 llvm::raw_string_ostream ArgOut(ArgString);
1084 Args[Arg].print(Policy, ArgOut);
1087 // If this is the first argument and its string representation
1088 // begins with the global scope specifier ('::foo'), add a space
1089 // to avoid printing the diagraph '<:'.
1090 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1093 SpecString += ArgString;
1096 // If the last character of our string is '>', add another space to
1097 // keep the two '>''s separate tokens. We don't *have* to do this in
1098 // C++0x, but it's still good hygiene.
1099 if (!SpecString.empty() && SpecString[SpecString.size() - 1] == '>')
1108 // Sadly, repeat all that with TemplateArgLoc.
1109 std::string TemplateSpecializationType::
1110 PrintTemplateArgumentList(const TemplateArgumentLoc *Args, unsigned NumArgs,
1111 const PrintingPolicy &Policy) {
1112 std::string SpecString;
1114 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1115 if (SpecString.size() > 1)
1118 // Print the argument into a string.
1119 std::string ArgString;
1120 if (Args[Arg].getArgument().getKind() == TemplateArgument::Pack) {
1121 ArgString = PrintTemplateArgumentList(
1122 Args[Arg].getArgument().pack_begin(),
1123 Args[Arg].getArgument().pack_size(),
1126 llvm::raw_string_ostream ArgOut(ArgString);
1127 Args[Arg].getArgument().print(Policy, ArgOut);
1130 // If this is the first argument and its string representation
1131 // begins with the global scope specifier ('::foo'), add a space
1132 // to avoid printing the diagraph '<:'.
1133 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1136 SpecString += ArgString;
1139 // If the last character of our string is '>', add another space to
1140 // keep the two '>''s separate tokens. We don't *have* to do this in
1141 // C++0x, but it's still good hygiene.
1142 if (SpecString[SpecString.size() - 1] == '>')
1150 void QualType::dump(const char *msg) const {
1151 std::string R = "identifier";
1153 getAsStringInternal(R, PrintingPolicy(LO));
1155 llvm::errs() << msg << ": ";
1156 llvm::errs() << R << "\n";
1158 void QualType::dump() const {
1162 void Type::dump() const {
1163 QualType(this, 0).dump();
1166 std::string Qualifiers::getAsString() const {
1168 return getAsString(PrintingPolicy(LO));
1171 // Appends qualifiers to the given string, separated by spaces. Will
1172 // prefix a space if the string is non-empty. Will not append a final
1174 void Qualifiers::getAsStringInternal(std::string &S,
1175 const PrintingPolicy& Policy) const {
1176 AppendTypeQualList(S, getCVRQualifiers());
1177 if (unsigned addrspace = getAddressSpace()) {
1178 if (!S.empty()) S += ' ';
1179 switch (addrspace) {
1180 case LangAS::opencl_global:
1183 case LangAS::opencl_local:
1186 case LangAS::opencl_constant:
1190 S += "__attribute__((address_space(";
1191 S += llvm::utostr_32(addrspace);
1195 if (Qualifiers::GC gc = getObjCGCAttr()) {
1196 if (!S.empty()) S += ' ';
1197 if (gc == Qualifiers::Weak)
1202 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) {
1204 !(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime))
1208 case Qualifiers::OCL_None: llvm_unreachable("none but true");
1209 case Qualifiers::OCL_ExplicitNone: S += "__unsafe_unretained"; break;
1210 case Qualifiers::OCL_Strong:
1211 if (!Policy.SuppressStrongLifetime)
1215 case Qualifiers::OCL_Weak: S += "__weak"; break;
1216 case Qualifiers::OCL_Autoreleasing: S += "__autoreleasing"; break;
1221 std::string QualType::getAsString(const Type *ty, Qualifiers qs) {
1223 LangOptions options;
1224 getAsStringInternal(ty, qs, buffer, PrintingPolicy(options));
1228 void QualType::getAsStringInternal(const Type *ty, Qualifiers qs,
1229 std::string &buffer,
1230 const PrintingPolicy &policy) {
1231 TypePrinter(policy).print(ty, qs, buffer);