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;
28 PrintingPolicy Policy;
31 explicit TypePrinter(const PrintingPolicy &Policy) : Policy(Policy) { }
33 void print(const Type *ty, Qualifiers qs, std::string &buffer);
34 void print(QualType T, std::string &S);
35 void AppendScope(DeclContext *DC, std::string &S);
36 void printTag(TagDecl *T, std::string &S);
37 #define ABSTRACT_TYPE(CLASS, PARENT)
38 #define TYPE(CLASS, PARENT) \
39 void print##CLASS(const CLASS##Type *T, std::string &S);
40 #include "clang/AST/TypeNodes.def"
44 static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
45 if (TypeQuals & Qualifiers::Const) {
46 if (!S.empty()) S += ' ';
49 if (TypeQuals & Qualifiers::Volatile) {
50 if (!S.empty()) S += ' ';
53 if (TypeQuals & Qualifiers::Restrict) {
54 if (!S.empty()) S += ' ';
59 void TypePrinter::print(QualType t, std::string &buffer) {
60 SplitQualType split = t.split();
61 print(split.first, split.second, buffer);
64 void TypePrinter::print(const Type *T, Qualifiers Quals, std::string &buffer) {
66 buffer += "NULL TYPE";
70 if (Policy.SuppressSpecifiers && T->isSpecifierType())
73 // Print qualifiers as appropriate.
75 // CanPrefixQualifiers - We prefer to print type qualifiers before the type,
76 // so that we get "const int" instead of "int const", but we can't do this if
77 // the type is complex. For example if the type is "int*", we *must* print
78 // "int * const", printing "const int *" is different. Only do this when the
79 // type expands to a simple string.
80 bool CanPrefixQualifiers = false;
82 Type::TypeClass TC = T->getTypeClass();
83 if (const AutoType *AT = dyn_cast<AutoType>(T))
84 TC = AT->desugar()->getTypeClass();
85 if (const SubstTemplateTypeParmType *Subst
86 = dyn_cast<SubstTemplateTypeParmType>(T))
87 TC = Subst->getReplacementType()->getTypeClass();
92 case Type::UnresolvedUsing:
94 case Type::TypeOfExpr:
99 case Type::Elaborated:
100 case Type::TemplateTypeParm:
101 case Type::SubstTemplateTypeParmPack:
102 case Type::TemplateSpecialization:
103 case Type::InjectedClassName:
104 case Type::DependentName:
105 case Type::DependentTemplateSpecialization:
106 case Type::ObjCObject:
107 case Type::ObjCInterface:
108 CanPrefixQualifiers = true;
111 case Type::ObjCObjectPointer:
112 CanPrefixQualifiers = T->isObjCIdType() || T->isObjCClassType() ||
113 T->isObjCQualifiedIdType() || T->isObjCQualifiedClassType();
117 case Type::BlockPointer:
118 case Type::LValueReference:
119 case Type::RValueReference:
120 case Type::MemberPointer:
121 case Type::ConstantArray:
122 case Type::IncompleteArray:
123 case Type::VariableArray:
124 case Type::DependentSizedArray:
125 case Type::DependentSizedExtVector:
127 case Type::ExtVector:
128 case Type::FunctionProto:
129 case Type::FunctionNoProto:
131 case Type::Attributed:
132 case Type::PackExpansion:
133 case Type::SubstTemplateTypeParm:
135 CanPrefixQualifiers = false;
139 if (!CanPrefixQualifiers && !Quals.empty()) {
140 std::string qualsBuffer;
141 Quals.getAsStringInternal(qualsBuffer, Policy);
143 if (!buffer.empty()) {
145 qualsBuffer += buffer;
147 std::swap(buffer, qualsBuffer);
150 switch (T->getTypeClass()) {
151 #define ABSTRACT_TYPE(CLASS, PARENT)
152 #define TYPE(CLASS, PARENT) case Type::CLASS: \
153 print##CLASS(cast<CLASS##Type>(T), buffer); \
155 #include "clang/AST/TypeNodes.def"
158 // If we're adding the qualifiers as a prefix, do it now.
159 if (CanPrefixQualifiers && !Quals.empty()) {
160 std::string qualsBuffer;
161 Quals.getAsStringInternal(qualsBuffer, Policy);
163 if (!buffer.empty()) {
165 qualsBuffer += buffer;
167 std::swap(buffer, qualsBuffer);
171 void TypePrinter::printBuiltin(const BuiltinType *T, std::string &S) {
173 S = T->getName(Policy.LangOpts);
175 // Prefix the basic type, e.g. 'int X'.
177 S = T->getName(Policy.LangOpts) + S;
181 void TypePrinter::printComplex(const ComplexType *T, std::string &S) {
182 print(T->getElementType(), S);
186 void TypePrinter::printPointer(const PointerType *T, std::string &S) {
189 // Handle things like 'int (*A)[4];' correctly.
190 // FIXME: this should include vectors, but vectors use attributes I guess.
191 if (isa<ArrayType>(T->getPointeeType()))
194 print(T->getPointeeType(), S);
197 void TypePrinter::printBlockPointer(const BlockPointerType *T, std::string &S) {
199 print(T->getPointeeType(), S);
202 void TypePrinter::printLValueReference(const LValueReferenceType *T,
206 // Handle things like 'int (&A)[4];' correctly.
207 // FIXME: this should include vectors, but vectors use attributes I guess.
208 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
211 print(T->getPointeeTypeAsWritten(), S);
214 void TypePrinter::printRValueReference(const RValueReferenceType *T,
218 // Handle things like 'int (&&A)[4];' correctly.
219 // FIXME: this should include vectors, but vectors use attributes I guess.
220 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
223 print(T->getPointeeTypeAsWritten(), S);
226 void TypePrinter::printMemberPointer(const MemberPointerType *T,
229 print(QualType(T->getClass(), 0), C);
233 // Handle things like 'int (Cls::*A)[4];' correctly.
234 // FIXME: this should include vectors, but vectors use attributes I guess.
235 if (isa<ArrayType>(T->getPointeeType()))
238 print(T->getPointeeType(), S);
241 void TypePrinter::printConstantArray(const ConstantArrayType *T,
244 S += llvm::utostr(T->getSize().getZExtValue());
247 print(T->getElementType(), S);
250 void TypePrinter::printIncompleteArray(const IncompleteArrayType *T,
253 print(T->getElementType(), S);
256 void TypePrinter::printVariableArray(const VariableArrayType *T,
260 if (T->getIndexTypeQualifiers().hasQualifiers()) {
261 AppendTypeQualList(S, T->getIndexTypeCVRQualifiers());
265 if (T->getSizeModifier() == VariableArrayType::Static)
267 else if (T->getSizeModifier() == VariableArrayType::Star)
270 if (T->getSizeExpr()) {
272 llvm::raw_string_ostream s(SStr);
273 T->getSizeExpr()->printPretty(s, 0, Policy);
278 print(T->getElementType(), S);
281 void TypePrinter::printDependentSizedArray(const DependentSizedArrayType *T,
285 if (T->getSizeExpr()) {
287 llvm::raw_string_ostream s(SStr);
288 T->getSizeExpr()->printPretty(s, 0, Policy);
293 print(T->getElementType(), S);
296 void TypePrinter::printDependentSizedExtVector(
297 const DependentSizedExtVectorType *T,
299 print(T->getElementType(), S);
301 S += " __attribute__((ext_vector_type(";
302 if (T->getSizeExpr()) {
304 llvm::raw_string_ostream s(SStr);
305 T->getSizeExpr()->printPretty(s, 0, Policy);
311 void TypePrinter::printVector(const VectorType *T, std::string &S) {
312 switch (T->getVectorKind()) {
313 case VectorType::AltiVecPixel:
314 S = "__vector __pixel " + S;
316 case VectorType::AltiVecBool:
317 print(T->getElementType(), S);
318 S = "__vector __bool " + S;
320 case VectorType::AltiVecVector:
321 print(T->getElementType(), S);
324 case VectorType::NeonVector:
325 print(T->getElementType(), S);
326 S = ("__attribute__((neon_vector_type(" +
327 llvm::utostr_32(T->getNumElements()) + "))) " + S);
329 case VectorType::NeonPolyVector:
330 print(T->getElementType(), S);
331 S = ("__attribute__((neon_polyvector_type(" +
332 llvm::utostr_32(T->getNumElements()) + "))) " + S);
334 case VectorType::GenericVector: {
335 // FIXME: We prefer to print the size directly here, but have no way
336 // to get the size of the type.
337 print(T->getElementType(), S);
338 std::string V = "__attribute__((__vector_size__(";
339 V += llvm::utostr_32(T->getNumElements()); // convert back to bytes.
341 print(T->getElementType(), ET);
342 V += " * sizeof(" + ET + ")))) ";
349 void TypePrinter::printExtVector(const ExtVectorType *T, std::string &S) {
350 S += " __attribute__((ext_vector_type(";
351 S += llvm::utostr_32(T->getNumElements());
353 print(T->getElementType(), S);
356 void TypePrinter::printFunctionProto(const FunctionProtoType *T,
358 // If needed for precedence reasons, wrap the inner part in grouping parens.
364 PrintingPolicy ParamPolicy(Policy);
365 ParamPolicy.SuppressSpecifiers = false;
366 for (unsigned i = 0, e = T->getNumArgs(); i != e; ++i) {
368 print(T->getArgType(i), Tmp);
373 if (T->isVariadic()) {
377 } else if (T->getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) {
378 // Do not emit int() if we have a proto, emit 'int(void)'.
384 FunctionType::ExtInfo Info = T->getExtInfo();
385 switch(Info.getCC()) {
389 S += " __attribute__((cdecl))";
392 S += " __attribute__((stdcall))";
395 S += " __attribute__((fastcall))";
398 S += " __attribute__((thiscall))";
401 S += " __attribute__((pascal))";
404 if (Info.getNoReturn())
405 S += " __attribute__((noreturn))";
406 if (Info.getRegParm())
407 S += " __attribute__((regparm (" +
408 llvm::utostr_32(Info.getRegParm()) + ")))";
410 AppendTypeQualList(S, T->getTypeQuals());
412 switch (T->getRefQualifier()) {
425 if (T->hasExceptionSpec()) {
427 if (T->hasAnyExceptionSpec())
430 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I) {
434 std::string ExceptionType;
435 print(T->getExceptionType(I), ExceptionType);
441 print(T->getResultType(), S);
444 void TypePrinter::printFunctionNoProto(const FunctionNoProtoType *T,
446 // If needed for precedence reasons, wrap the inner part in grouping parens.
451 if (T->getNoReturnAttr())
452 S += " __attribute__((noreturn))";
453 print(T->getResultType(), S);
456 static void printTypeSpec(const NamedDecl *D, std::string &S) {
457 IdentifierInfo *II = D->getIdentifier();
459 S = II->getName().str();
461 S = II->getName().str() + ' ' + S;
464 void TypePrinter::printUnresolvedUsing(const UnresolvedUsingType *T,
466 printTypeSpec(T->getDecl(), S);
469 void TypePrinter::printTypedef(const TypedefType *T, std::string &S) {
470 printTypeSpec(T->getDecl(), S);
473 void TypePrinter::printTypeOfExpr(const TypeOfExprType *T, std::string &S) {
474 if (!S.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
477 llvm::raw_string_ostream s(Str);
478 T->getUnderlyingExpr()->printPretty(s, 0, Policy);
479 S = "typeof " + s.str() + S;
482 void TypePrinter::printTypeOf(const TypeOfType *T, std::string &S) {
483 if (!S.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
486 print(T->getUnderlyingType(), Tmp);
487 S = "typeof(" + Tmp + ")" + S;
490 void TypePrinter::printDecltype(const DecltypeType *T, std::string &S) {
491 if (!S.empty()) // Prefix the basic type, e.g. 'decltype(t) X'.
494 llvm::raw_string_ostream s(Str);
495 T->getUnderlyingExpr()->printPretty(s, 0, Policy);
496 S = "decltype(" + s.str() + ")" + S;
499 void TypePrinter::printAuto(const AutoType *T, std::string &S) {
500 // If the type has been deduced, do not print 'auto'.
501 if (T->isDeduced()) {
502 print(T->getDeducedType(), S);
504 if (!S.empty()) // Prefix the basic type, e.g. 'auto X'.
510 /// Appends the given scope to the end of a string.
511 void TypePrinter::AppendScope(DeclContext *DC, std::string &Buffer) {
512 if (DC->isTranslationUnit()) return;
513 AppendScope(DC->getParent(), Buffer);
515 unsigned OldSize = Buffer.size();
517 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
518 if (NS->getIdentifier())
519 Buffer += NS->getNameAsString();
521 Buffer += "<anonymous>";
522 } else if (ClassTemplateSpecializationDecl *Spec
523 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
524 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
525 std::string TemplateArgsStr
526 = TemplateSpecializationType::PrintTemplateArgumentList(
530 Buffer += Spec->getIdentifier()->getName();
531 Buffer += TemplateArgsStr;
532 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
533 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
534 Buffer += Typedef->getIdentifier()->getName();
535 else if (Tag->getIdentifier())
536 Buffer += Tag->getIdentifier()->getName();
539 if (Buffer.size() != OldSize)
543 void TypePrinter::printTag(TagDecl *D, std::string &InnerString) {
544 if (Policy.SuppressTag)
548 bool HasKindDecoration = false;
550 // We don't print tags unless this is an elaborated type.
551 // In C, we just assume every RecordType is an elaborated type.
552 if (!Policy.LangOpts.CPlusPlus && !D->getTypedefForAnonDecl()) {
553 HasKindDecoration = true;
554 Buffer += D->getKindName();
558 // Compute the full nested-name-specifier for this type.
559 // In C, this will always be empty except when the type
560 // being printed is anonymous within other Record.
561 if (!Policy.SuppressScope)
562 AppendScope(D->getDeclContext(), Buffer);
564 if (const IdentifierInfo *II = D->getIdentifier())
565 Buffer += II->getNameStart();
566 else if (TypedefDecl *Typedef = D->getTypedefForAnonDecl()) {
567 assert(Typedef->getIdentifier() && "Typedef without identifier?");
568 Buffer += Typedef->getIdentifier()->getNameStart();
570 // Make an unambiguous representation for anonymous types, e.g.
571 // <anonymous enum at /usr/include/string.h:120:9>
572 llvm::raw_string_ostream OS(Buffer);
575 if (Policy.AnonymousTagLocations) {
576 // Suppress the redundant tag keyword if we just printed one.
577 // We don't have to worry about ElaboratedTypes here because you can't
578 // refer to an anonymous type with one.
579 if (!HasKindDecoration)
580 OS << " " << D->getKindName();
582 PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc(
584 if (PLoc.isValid()) {
585 OS << " at " << PLoc.getFilename()
586 << ':' << PLoc.getLine()
587 << ':' << PLoc.getColumn();
594 // If this is a class template specialization, print the template
596 if (ClassTemplateSpecializationDecl *Spec
597 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
598 const TemplateArgument *Args;
600 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
601 const TemplateSpecializationType *TST =
602 cast<TemplateSpecializationType>(TAW->getType());
603 Args = TST->getArgs();
604 NumArgs = TST->getNumArgs();
606 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
607 Args = TemplateArgs.data();
608 NumArgs = TemplateArgs.size();
610 Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
615 if (!InnerString.empty()) {
617 Buffer += InnerString;
620 std::swap(Buffer, InnerString);
623 void TypePrinter::printRecord(const RecordType *T, std::string &S) {
624 printTag(T->getDecl(), S);
627 void TypePrinter::printEnum(const EnumType *T, std::string &S) {
628 printTag(T->getDecl(), S);
631 void TypePrinter::printTemplateTypeParm(const TemplateTypeParmType *T,
633 if (!S.empty()) // Prefix the basic type, e.g. 'parmname X'.
637 S = "type-parameter-" + llvm::utostr_32(T->getDepth()) + '-' +
638 llvm::utostr_32(T->getIndex()) + S;
640 S = T->getName()->getName().str() + S;
643 void TypePrinter::printSubstTemplateTypeParm(const SubstTemplateTypeParmType *T,
645 print(T->getReplacementType(), S);
648 void TypePrinter::printSubstTemplateTypeParmPack(
649 const SubstTemplateTypeParmPackType *T,
651 printTemplateTypeParm(T->getReplacedParameter(), S);
654 void TypePrinter::printTemplateSpecialization(
655 const TemplateSpecializationType *T,
657 std::string SpecString;
660 llvm::raw_string_ostream OS(SpecString);
661 T->getTemplateName().print(OS, Policy);
664 SpecString += TemplateSpecializationType::PrintTemplateArgumentList(
671 S = SpecString + ' ' + S;
674 void TypePrinter::printInjectedClassName(const InjectedClassNameType *T,
676 printTemplateSpecialization(T->getInjectedTST(), S);
679 void TypePrinter::printElaborated(const ElaboratedType *T, std::string &S) {
680 std::string MyString;
683 llvm::raw_string_ostream OS(MyString);
684 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
685 if (T->getKeyword() != ETK_None)
687 NestedNameSpecifier* Qualifier = T->getQualifier();
689 Qualifier->print(OS, Policy);
693 PrintingPolicy InnerPolicy(Policy);
694 InnerPolicy.SuppressScope = true;
695 TypePrinter(InnerPolicy).print(T->getNamedType(), TypeStr);
701 S = MyString + ' ' + S;
704 void TypePrinter::printParen(const ParenType *T, std::string &S) {
705 if (!S.empty() && !isa<FunctionType>(T->getInnerType()))
707 print(T->getInnerType(), S);
710 void TypePrinter::printDependentName(const DependentNameType *T, std::string &S) {
711 std::string MyString;
714 llvm::raw_string_ostream OS(MyString);
715 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
716 if (T->getKeyword() != ETK_None)
719 T->getQualifier()->print(OS, Policy);
721 OS << T->getIdentifier()->getName();
727 S = MyString + ' ' + S;
730 void TypePrinter::printDependentTemplateSpecialization(
731 const DependentTemplateSpecializationType *T, std::string &S) {
732 std::string MyString;
734 llvm::raw_string_ostream OS(MyString);
736 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
737 if (T->getKeyword() != ETK_None)
740 T->getQualifier()->print(OS, Policy);
741 OS << T->getIdentifier()->getName();
742 OS << TemplateSpecializationType::PrintTemplateArgumentList(
751 S = MyString + ' ' + S;
754 void TypePrinter::printPackExpansion(const PackExpansionType *T,
756 print(T->getPattern(), S);
760 void TypePrinter::printAttributed(const AttributedType *T,
762 print(T->getModifiedType(), S);
764 // TODO: not all attributes are GCC-style attributes.
765 S += "__attribute__((";
766 switch (T->getAttrKind()) {
767 case AttributedType::attr_address_space:
768 S += "address_space(";
769 S += T->getEquivalentType().getAddressSpace();
773 case AttributedType::attr_vector_size: {
774 S += "__vector_size__(";
775 if (const VectorType *vector =T->getEquivalentType()->getAs<VectorType>()) {
776 S += vector->getNumElements();
780 print(vector->getElementType(), tmp);
788 case AttributedType::attr_neon_vector_type:
789 case AttributedType::attr_neon_polyvector_type: {
790 if (T->getAttrKind() == AttributedType::attr_neon_vector_type)
791 S += "neon_vector_type(";
793 S += "neon_polyvector_type(";
794 const VectorType *vector = T->getEquivalentType()->getAs<VectorType>();
795 S += llvm::utostr_32(vector->getNumElements());
800 case AttributedType::attr_regparm: {
802 QualType t = T->getEquivalentType();
803 while (!t->isFunctionType())
804 t = t->getPointeeType();
805 S += t->getAs<FunctionType>()->getRegParmType();
810 case AttributedType::attr_objc_gc: {
813 QualType tmp = T->getEquivalentType();
814 while (tmp.getObjCGCAttr() == Qualifiers::GCNone) {
815 QualType next = tmp->getPointeeType();
816 if (next == tmp) break;
820 if (tmp.isObjCGCWeak())
828 case AttributedType::attr_noreturn: S += "noreturn"; break;
829 case AttributedType::attr_cdecl: S += "cdecl"; break;
830 case AttributedType::attr_fastcall: S += "fastcall"; break;
831 case AttributedType::attr_stdcall: S += "stdcall"; break;
832 case AttributedType::attr_thiscall: S += "thiscall"; break;
833 case AttributedType::attr_pascal: S += "pascal"; break;
838 void TypePrinter::printObjCInterface(const ObjCInterfaceType *T,
840 if (!S.empty()) // Prefix the basic type, e.g. 'typedefname X'.
843 std::string ObjCQIString = T->getDecl()->getNameAsString();
844 S = ObjCQIString + S;
847 void TypePrinter::printObjCObject(const ObjCObjectType *T,
850 return print(T->getBaseType(), S);
853 print(T->getBaseType(), tmp);
856 for (ObjCObjectType::qual_iterator
857 I = T->qual_begin(), E = T->qual_end(); I != E; ++I) {
862 tmp += (*I)->getNameAsString();
873 void TypePrinter::printObjCObjectPointer(const ObjCObjectPointerType *T,
875 std::string ObjCQIString;
877 T->getPointeeType().getLocalQualifiers().getAsStringInternal(ObjCQIString,
879 if (!ObjCQIString.empty())
882 if (T->isObjCIdType() || T->isObjCQualifiedIdType())
883 ObjCQIString += "id";
884 else if (T->isObjCClassType() || T->isObjCQualifiedClassType())
885 ObjCQIString += "Class";
886 else if (T->isObjCSelType())
887 ObjCQIString += "SEL";
889 ObjCQIString += T->getInterfaceDecl()->getNameAsString();
891 if (!T->qual_empty()) {
893 for (ObjCObjectPointerType::qual_iterator I = T->qual_begin(),
896 ObjCQIString += (*I)->getNameAsString();
903 if (!T->isObjCIdType() && !T->isObjCQualifiedIdType())
904 ObjCQIString += " *"; // Don't forget the implicit pointer.
905 else if (!S.empty()) // Prefix the basic type, e.g. 'typedefname X'.
908 S = ObjCQIString + S;
911 std::string TemplateSpecializationType::
912 PrintTemplateArgumentList(const TemplateArgumentListInfo &Args,
913 const PrintingPolicy &Policy) {
914 return PrintTemplateArgumentList(Args.getArgumentArray(),
920 TemplateSpecializationType::PrintTemplateArgumentList(
921 const TemplateArgument *Args,
923 const PrintingPolicy &Policy,
925 std::string SpecString;
929 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
930 if (SpecString.size() > !SkipBrackets)
933 // Print the argument into a string.
934 std::string ArgString;
935 if (Args[Arg].getKind() == TemplateArgument::Pack) {
936 ArgString = PrintTemplateArgumentList(Args[Arg].pack_begin(),
937 Args[Arg].pack_size(),
940 llvm::raw_string_ostream ArgOut(ArgString);
941 Args[Arg].print(Policy, ArgOut);
944 // If this is the first argument and its string representation
945 // begins with the global scope specifier ('::foo'), add a space
946 // to avoid printing the diagraph '<:'.
947 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
950 SpecString += ArgString;
953 // If the last character of our string is '>', add another space to
954 // keep the two '>''s separate tokens. We don't *have* to do this in
955 // C++0x, but it's still good hygiene.
956 if (!SpecString.empty() && SpecString[SpecString.size() - 1] == '>')
965 // Sadly, repeat all that with TemplateArgLoc.
966 std::string TemplateSpecializationType::
967 PrintTemplateArgumentList(const TemplateArgumentLoc *Args, unsigned NumArgs,
968 const PrintingPolicy &Policy) {
969 std::string SpecString;
971 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
972 if (SpecString.size() > 1)
975 // Print the argument into a string.
976 std::string ArgString;
977 if (Args[Arg].getArgument().getKind() == TemplateArgument::Pack) {
978 ArgString = PrintTemplateArgumentList(
979 Args[Arg].getArgument().pack_begin(),
980 Args[Arg].getArgument().pack_size(),
983 llvm::raw_string_ostream ArgOut(ArgString);
984 Args[Arg].getArgument().print(Policy, ArgOut);
987 // If this is the first argument and its string representation
988 // begins with the global scope specifier ('::foo'), add a space
989 // to avoid printing the diagraph '<:'.
990 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
993 SpecString += ArgString;
996 // If the last character of our string is '>', add another space to
997 // keep the two '>''s separate tokens. We don't *have* to do this in
998 // C++0x, but it's still good hygiene.
999 if (SpecString[SpecString.size() - 1] == '>')
1007 void QualType::dump(const char *msg) const {
1008 std::string R = "identifier";
1010 getAsStringInternal(R, PrintingPolicy(LO));
1012 llvm::errs() << msg << ": ";
1013 llvm::errs() << R << "\n";
1015 void QualType::dump() const {
1019 void Type::dump() const {
1020 QualType(this, 0).dump();
1023 std::string Qualifiers::getAsString() const {
1025 return getAsString(PrintingPolicy(LO));
1028 // Appends qualifiers to the given string, separated by spaces. Will
1029 // prefix a space if the string is non-empty. Will not append a final
1031 void Qualifiers::getAsStringInternal(std::string &S,
1032 const PrintingPolicy&) const {
1033 AppendTypeQualList(S, getCVRQualifiers());
1034 if (unsigned AddressSpace = getAddressSpace()) {
1035 if (!S.empty()) S += ' ';
1036 S += "__attribute__((address_space(";
1037 S += llvm::utostr_32(AddressSpace);
1040 if (Qualifiers::GC GCAttrType = getObjCGCAttr()) {
1041 if (!S.empty()) S += ' ';
1042 S += "__attribute__((objc_gc(";
1043 if (GCAttrType == Qualifiers::Weak)
1051 std::string QualType::getAsString(const Type *ty, Qualifiers qs) {
1053 LangOptions options;
1054 getAsStringInternal(ty, qs, buffer, PrintingPolicy(options));
1058 void QualType::getAsStringInternal(const Type *ty, Qualifiers qs,
1059 std::string &buffer,
1060 const PrintingPolicy &policy) {
1061 TypePrinter(policy).print(ty, qs, buffer);