Home | History | Annotate | Download | only in AST
      1 //===--- TypePrinter.cpp - Pretty-Print Clang Types -----------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This contains code to print types from Clang's type system.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "clang/AST/Decl.h"
     15 #include "clang/AST/DeclObjC.h"
     16 #include "clang/AST/DeclTemplate.h"
     17 #include "clang/AST/Expr.h"
     18 #include "clang/AST/Type.h"
     19 #include "clang/AST/PrettyPrinter.h"
     20 #include "clang/Basic/LangOptions.h"
     21 #include "clang/Basic/SourceManager.h"
     22 #include "llvm/ADT/StringExtras.h"
     23 #include "llvm/Support/raw_ostream.h"
     24 using namespace clang;
     25 
     26 namespace {
     27   /// \brief RAII object that enables printing of the ARC __strong lifetime
     28   /// qualifier.
     29   class IncludeStrongLifetimeRAII {
     30     PrintingPolicy &Policy;
     31     bool Old;
     32 
     33   public:
     34     explicit IncludeStrongLifetimeRAII(PrintingPolicy &Policy)
     35       : Policy(Policy), Old(Policy.SuppressStrongLifetime) {
     36       Policy.SuppressStrongLifetime = false;
     37     }
     38 
     39     ~IncludeStrongLifetimeRAII() {
     40       Policy.SuppressStrongLifetime = Old;
     41     }
     42   };
     43 
     44   class TypePrinter {
     45     PrintingPolicy Policy;
     46 
     47   public:
     48     explicit TypePrinter(const PrintingPolicy &Policy) : Policy(Policy) { }
     49 
     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"
     58   };
     59 }
     60 
     61 static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
     62   if (TypeQuals & Qualifiers::Const) {
     63     if (!S.empty()) S += ' ';
     64     S += "const";
     65   }
     66   if (TypeQuals & Qualifiers::Volatile) {
     67     if (!S.empty()) S += ' ';
     68     S += "volatile";
     69   }
     70   if (TypeQuals & Qualifiers::Restrict) {
     71     if (!S.empty()) S += ' ';
     72     S += "restrict";
     73   }
     74 }
     75 
     76 void TypePrinter::print(QualType t, std::string &buffer) {
     77   SplitQualType split = t.split();
     78   print(split.Ty, split.Quals, buffer);
     79 }
     80 
     81 void TypePrinter::print(const Type *T, Qualifiers Quals, std::string &buffer) {
     82   if (!T) {
     83     buffer += "NULL TYPE";
     84     return;
     85   }
     86 
     87   if (Policy.SuppressSpecifiers && T->isSpecifierType())
     88     return;
     89 
     90   // Print qualifiers as appropriate.
     91 
     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();
    105 
    106   switch (TC) {
    107     case Type::Builtin:
    108     case Type::Complex:
    109     case Type::UnresolvedUsing:
    110     case Type::Typedef:
    111     case Type::TypeOfExpr:
    112     case Type::TypeOf:
    113     case Type::Decltype:
    114     case Type::UnaryTransform:
    115     case Type::Record:
    116     case Type::Enum:
    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     case Type::Atomic:
    127       CanPrefixQualifiers = true;
    128       break;
    129 
    130     case Type::ObjCObjectPointer:
    131       CanPrefixQualifiers = T->isObjCIdType() || T->isObjCClassType() ||
    132         T->isObjCQualifiedIdType() || T->isObjCQualifiedClassType();
    133       break;
    134 
    135     case Type::ConstantArray:
    136     case Type::IncompleteArray:
    137     case Type::VariableArray:
    138     case Type::DependentSizedArray:
    139       NeedARCStrongQualifier = true;
    140       // Fall through
    141 
    142     case Type::Pointer:
    143     case Type::BlockPointer:
    144     case Type::LValueReference:
    145     case Type::RValueReference:
    146     case Type::MemberPointer:
    147     case Type::DependentSizedExtVector:
    148     case Type::Vector:
    149     case Type::ExtVector:
    150     case Type::FunctionProto:
    151     case Type::FunctionNoProto:
    152     case Type::Paren:
    153     case Type::Attributed:
    154     case Type::PackExpansion:
    155     case Type::SubstTemplateTypeParm:
    156     case Type::Auto:
    157       CanPrefixQualifiers = false;
    158       break;
    159   }
    160 
    161   if (!CanPrefixQualifiers && !Quals.empty()) {
    162     std::string qualsBuffer;
    163     if (NeedARCStrongQualifier) {
    164       IncludeStrongLifetimeRAII Strong(Policy);
    165       Quals.getAsStringInternal(qualsBuffer, Policy);
    166     } else {
    167       Quals.getAsStringInternal(qualsBuffer, Policy);
    168     }
    169 
    170     if (!qualsBuffer.empty()) {
    171       if (!buffer.empty()) {
    172         qualsBuffer += ' ';
    173         qualsBuffer += buffer;
    174       }
    175       std::swap(buffer, qualsBuffer);
    176     }
    177   }
    178 
    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); \
    183     break;
    184 #include "clang/AST/TypeNodes.def"
    185   }
    186 
    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);
    193     } else {
    194       Quals.getAsStringInternal(qualsBuffer, Policy);
    195     }
    196 
    197     if (!qualsBuffer.empty()) {
    198       if (!buffer.empty()) {
    199         qualsBuffer += ' ';
    200         qualsBuffer += buffer;
    201       }
    202       std::swap(buffer, qualsBuffer);
    203     }
    204   }
    205 }
    206 
    207 void TypePrinter::printBuiltin(const BuiltinType *T, std::string &S) {
    208   if (S.empty()) {
    209     S = T->getName(Policy);
    210   } else {
    211     // Prefix the basic type, e.g. 'int X'.
    212     S = ' ' + S;
    213     S = T->getName(Policy) + S;
    214   }
    215 }
    216 
    217 void TypePrinter::printComplex(const ComplexType *T, std::string &S) {
    218   print(T->getElementType(), S);
    219   S = "_Complex " + S;
    220 }
    221 
    222 void TypePrinter::printPointer(const PointerType *T, std::string &S) {
    223   S = '*' + S;
    224 
    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()))
    228     S = '(' + S + ')';
    229 
    230   IncludeStrongLifetimeRAII Strong(Policy);
    231   print(T->getPointeeType(), S);
    232 }
    233 
    234 void TypePrinter::printBlockPointer(const BlockPointerType *T, std::string &S) {
    235   S = '^' + S;
    236   print(T->getPointeeType(), S);
    237 }
    238 
    239 void TypePrinter::printLValueReference(const LValueReferenceType *T,
    240                                        std::string &S) {
    241   S = '&' + S;
    242 
    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()))
    246     S = '(' + S + ')';
    247 
    248   IncludeStrongLifetimeRAII Strong(Policy);
    249   print(T->getPointeeTypeAsWritten(), S);
    250 }
    251 
    252 void TypePrinter::printRValueReference(const RValueReferenceType *T,
    253                                        std::string &S) {
    254   S = "&&" + S;
    255 
    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()))
    259     S = '(' + S + ')';
    260 
    261   IncludeStrongLifetimeRAII Strong(Policy);
    262   print(T->getPointeeTypeAsWritten(), S);
    263 }
    264 
    265 void TypePrinter::printMemberPointer(const MemberPointerType *T,
    266                                      std::string &S) {
    267   PrintingPolicy InnerPolicy(Policy);
    268   Policy.SuppressTag = true;
    269   std::string C = QualType(T->getClass(), 0).getAsString(InnerPolicy);
    270   C += "::*";
    271   S = C + S;
    272 
    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()))
    276     S = '(' + S + ')';
    277 
    278   IncludeStrongLifetimeRAII Strong(Policy);
    279   print(T->getPointeeType(), S);
    280 }
    281 
    282 void TypePrinter::printConstantArray(const ConstantArrayType *T,
    283                                      std::string &S) {
    284   S += '[';
    285   S += llvm::utostr(T->getSize().getZExtValue());
    286   S += ']';
    287 
    288   IncludeStrongLifetimeRAII Strong(Policy);
    289   print(T->getElementType(), S);
    290 }
    291 
    292 void TypePrinter::printIncompleteArray(const IncompleteArrayType *T,
    293                                        std::string &S) {
    294   S += "[]";
    295   IncludeStrongLifetimeRAII Strong(Policy);
    296   print(T->getElementType(), S);
    297 }
    298 
    299 void TypePrinter::printVariableArray(const VariableArrayType *T,
    300                                      std::string &S) {
    301   S += '[';
    302 
    303   if (T->getIndexTypeQualifiers().hasQualifiers()) {
    304     AppendTypeQualList(S, T->getIndexTypeCVRQualifiers());
    305     S += ' ';
    306   }
    307 
    308   if (T->getSizeModifier() == VariableArrayType::Static)
    309     S += "static";
    310   else if (T->getSizeModifier() == VariableArrayType::Star)
    311     S += '*';
    312 
    313   if (T->getSizeExpr()) {
    314     std::string SStr;
    315     llvm::raw_string_ostream s(SStr);
    316     T->getSizeExpr()->printPretty(s, 0, Policy);
    317     S += s.str();
    318   }
    319   S += ']';
    320 
    321   IncludeStrongLifetimeRAII Strong(Policy);
    322   print(T->getElementType(), S);
    323 }
    324 
    325 void TypePrinter::printDependentSizedArray(const DependentSizedArrayType *T,
    326                                            std::string &S) {
    327   S += '[';
    328 
    329   if (T->getSizeExpr()) {
    330     std::string SStr;
    331     llvm::raw_string_ostream s(SStr);
    332     T->getSizeExpr()->printPretty(s, 0, Policy);
    333     S += s.str();
    334   }
    335   S += ']';
    336 
    337   IncludeStrongLifetimeRAII Strong(Policy);
    338   print(T->getElementType(), S);
    339 }
    340 
    341 void TypePrinter::printDependentSizedExtVector(
    342                                           const DependentSizedExtVectorType *T,
    343                                                std::string &S) {
    344   print(T->getElementType(), S);
    345 
    346   S += " __attribute__((ext_vector_type(";
    347   if (T->getSizeExpr()) {
    348     std::string SStr;
    349     llvm::raw_string_ostream s(SStr);
    350     T->getSizeExpr()->printPretty(s, 0, Policy);
    351     S += s.str();
    352   }
    353   S += ")))";
    354 }
    355 
    356 void TypePrinter::printVector(const VectorType *T, std::string &S) {
    357   switch (T->getVectorKind()) {
    358   case VectorType::AltiVecPixel:
    359     S = "__vector __pixel " + S;
    360     break;
    361   case VectorType::AltiVecBool:
    362     print(T->getElementType(), S);
    363     S = "__vector __bool " + S;
    364     break;
    365   case VectorType::AltiVecVector:
    366     print(T->getElementType(), S);
    367     S = "__vector " + S;
    368     break;
    369   case VectorType::NeonVector:
    370     print(T->getElementType(), S);
    371     S = ("__attribute__((neon_vector_type(" +
    372          llvm::utostr_32(T->getNumElements()) + "))) " + S);
    373     break;
    374   case VectorType::NeonPolyVector:
    375     print(T->getElementType(), S);
    376     S = ("__attribute__((neon_polyvector_type(" +
    377          llvm::utostr_32(T->getNumElements()) + "))) " + S);
    378     break;
    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.
    385     std::string ET;
    386     print(T->getElementType(), ET);
    387     V += " * sizeof(" + ET + ")))) ";
    388     S = V + S;
    389     break;
    390   }
    391   }
    392 }
    393 
    394 void TypePrinter::printExtVector(const ExtVectorType *T, std::string &S) {
    395   S += " __attribute__((ext_vector_type(";
    396   S += llvm::utostr_32(T->getNumElements());
    397   S += ")))";
    398   print(T->getElementType(), S);
    399 }
    400 
    401 void
    402 FunctionProtoType::printExceptionSpecification(std::string &S,
    403                                                PrintingPolicy Policy) const {
    404 
    405   if (hasDynamicExceptionSpec()) {
    406     S += " throw(";
    407     if (getExceptionSpecType() == EST_MSAny)
    408       S += "...";
    409     else
    410       for (unsigned I = 0, N = getNumExceptions(); I != N; ++I) {
    411         if (I)
    412           S += ", ";
    413 
    414         S += getExceptionType(I).getAsString(Policy);
    415       }
    416     S += ")";
    417   } else if (isNoexceptExceptionSpec(getExceptionSpecType())) {
    418     S += " noexcept";
    419     if (getExceptionSpecType() == EST_ComputedNoexcept) {
    420       S += "(";
    421       llvm::raw_string_ostream EOut(S);
    422       getNoexceptExpr()->printPretty(EOut, 0, Policy);
    423       EOut.flush();
    424       S += EOut.str();
    425       S += ")";
    426     }
    427   }
    428 }
    429 
    430 void TypePrinter::printFunctionProto(const FunctionProtoType *T,
    431                                      std::string &S) {
    432   // If needed for precedence reasons, wrap the inner part in grouping parens.
    433   if (!S.empty())
    434     S = "(" + S + ")";
    435 
    436   S += "(";
    437   std::string Tmp;
    438   PrintingPolicy ParamPolicy(Policy);
    439   ParamPolicy.SuppressSpecifiers = false;
    440   for (unsigned i = 0, e = T->getNumArgs(); i != e; ++i) {
    441     if (i) S += ", ";
    442     print(T->getArgType(i), Tmp);
    443     S += Tmp;
    444     Tmp.clear();
    445   }
    446 
    447   if (T->isVariadic()) {
    448     if (T->getNumArgs())
    449       S += ", ";
    450     S += "...";
    451   } else if (T->getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) {
    452     // Do not emit int() if we have a proto, emit 'int(void)'.
    453     S += "void";
    454   }
    455 
    456   S += ")";
    457 
    458   FunctionType::ExtInfo Info = T->getExtInfo();
    459   switch(Info.getCC()) {
    460   case CC_Default: break;
    461   case CC_C:
    462     S += " __attribute__((cdecl))";
    463     break;
    464   case CC_X86StdCall:
    465     S += " __attribute__((stdcall))";
    466     break;
    467   case CC_X86FastCall:
    468     S += " __attribute__((fastcall))";
    469     break;
    470   case CC_X86ThisCall:
    471     S += " __attribute__((thiscall))";
    472     break;
    473   case CC_X86Pascal:
    474     S += " __attribute__((pascal))";
    475     break;
    476   case CC_AAPCS:
    477     S += " __attribute__((pcs(\"aapcs\")))";
    478     break;
    479   case CC_AAPCS_VFP:
    480     S += " __attribute__((pcs(\"aapcs-vfp\")))";
    481     break;
    482   }
    483   if (Info.getNoReturn())
    484     S += " __attribute__((noreturn))";
    485   if (Info.getRegParm())
    486     S += " __attribute__((regparm (" +
    487         llvm::utostr_32(Info.getRegParm()) + ")))";
    488 
    489   AppendTypeQualList(S, T->getTypeQuals());
    490 
    491   switch (T->getRefQualifier()) {
    492   case RQ_None:
    493     break;
    494 
    495   case RQ_LValue:
    496     S += " &";
    497     break;
    498 
    499   case RQ_RValue:
    500     S += " &&";
    501     break;
    502   }
    503   T->printExceptionSpecification(S, Policy);
    504   if (T->hasTrailingReturn()) {
    505     std::string ResultS;
    506     print(T->getResultType(), ResultS);
    507     S = "auto " + S + " -> " + ResultS;
    508   } else
    509     print(T->getResultType(), S);
    510 }
    511 
    512 void TypePrinter::printFunctionNoProto(const FunctionNoProtoType *T,
    513                                        std::string &S) {
    514   // If needed for precedence reasons, wrap the inner part in grouping parens.
    515   if (!S.empty())
    516     S = "(" + S + ")";
    517 
    518   S += "()";
    519   if (T->getNoReturnAttr())
    520     S += " __attribute__((noreturn))";
    521   print(T->getResultType(), S);
    522 }
    523 
    524 static void printTypeSpec(const NamedDecl *D, std::string &S) {
    525   IdentifierInfo *II = D->getIdentifier();
    526   if (S.empty())
    527     S = II->getName().str();
    528   else
    529     S = II->getName().str() + ' ' + S;
    530 }
    531 
    532 void TypePrinter::printUnresolvedUsing(const UnresolvedUsingType *T,
    533                                        std::string &S) {
    534   printTypeSpec(T->getDecl(), S);
    535 }
    536 
    537 void TypePrinter::printTypedef(const TypedefType *T, std::string &S) {
    538   printTypeSpec(T->getDecl(), S);
    539 }
    540 
    541 void TypePrinter::printTypeOfExpr(const TypeOfExprType *T, std::string &S) {
    542   if (!S.empty())    // Prefix the basic type, e.g. 'typeof(e) X'.
    543     S = ' ' + S;
    544   std::string Str;
    545   llvm::raw_string_ostream s(Str);
    546   T->getUnderlyingExpr()->printPretty(s, 0, Policy);
    547   S = "typeof " + s.str() + S;
    548 }
    549 
    550 void TypePrinter::printTypeOf(const TypeOfType *T, std::string &S) {
    551   if (!S.empty())    // Prefix the basic type, e.g. 'typeof(t) X'.
    552     S = ' ' + S;
    553   std::string Tmp;
    554   print(T->getUnderlyingType(), Tmp);
    555   S = "typeof(" + Tmp + ")" + S;
    556 }
    557 
    558 void TypePrinter::printDecltype(const DecltypeType *T, std::string &S) {
    559   if (!S.empty())    // Prefix the basic type, e.g. 'decltype(t) X'.
    560     S = ' ' + S;
    561   std::string Str;
    562   llvm::raw_string_ostream s(Str);
    563   T->getUnderlyingExpr()->printPretty(s, 0, Policy);
    564   S = "decltype(" + s.str() + ")" + S;
    565 }
    566 
    567 void TypePrinter::printUnaryTransform(const UnaryTransformType *T,
    568                                            std::string &S) {
    569   if (!S.empty())
    570     S = ' ' + S;
    571   std::string Str;
    572   IncludeStrongLifetimeRAII Strong(Policy);
    573   print(T->getBaseType(), Str);
    574 
    575   switch (T->getUTTKind()) {
    576     case UnaryTransformType::EnumUnderlyingType:
    577       S = "__underlying_type(" + Str + ")" + S;
    578       break;
    579   }
    580 }
    581 
    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);
    586   } else {
    587     if (!S.empty())    // Prefix the basic type, e.g. 'auto X'.
    588       S = ' ' + S;
    589     S = "auto" + S;
    590   }
    591 }
    592 
    593 void TypePrinter::printAtomic(const AtomicType *T, std::string &S) {
    594   if (!S.empty())
    595     S = ' ' + S;
    596   std::string Str;
    597   IncludeStrongLifetimeRAII Strong(Policy);
    598   print(T->getValueType(), Str);
    599 
    600   S = "_Atomic(" + Str + ")" + S;
    601 }
    602 
    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);
    607 
    608   unsigned OldSize = Buffer.size();
    609 
    610   if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
    611     if (Policy.SuppressUnwrittenScope &&
    612         (NS->isAnonymousNamespace() || NS->isInline()))
    613       return;
    614     if (NS->getIdentifier())
    615       Buffer += NS->getNameAsString();
    616     else
    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(
    624                                             TemplateArgs.data(),
    625                                             TemplateArgs.size(),
    626                                             Policy);
    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();
    634     else
    635       return;
    636   }
    637 
    638   if (Buffer.size() != OldSize)
    639     Buffer += "::";
    640 }
    641 
    642 void TypePrinter::printTag(TagDecl *D, std::string &InnerString) {
    643   if (Policy.SuppressTag)
    644     return;
    645 
    646   std::string Buffer;
    647   bool HasKindDecoration = false;
    648 
    649   // bool SuppressTagKeyword
    650   //   = Policy.LangOpts.CPlusPlus || Policy.SuppressTagKeyword;
    651 
    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();
    658     Buffer += ' ';
    659   }
    660 
    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);
    666 
    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();
    672   } else {
    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);
    676 
    677     if (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda()) {
    678       OS << "<lambda";
    679       HasKindDecoration = true;
    680     } else {
    681       OS << "<anonymous";
    682     }
    683 
    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();
    690 
    691       PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc(
    692           D->getLocation());
    693       if (PLoc.isValid()) {
    694         OS << " at " << PLoc.getFilename()
    695            << ':' << PLoc.getLine()
    696            << ':' << PLoc.getColumn();
    697       }
    698     }
    699 
    700     OS << '>';
    701   }
    702 
    703   // If this is a class template specialization, print the template
    704   // arguments.
    705   if (ClassTemplateSpecializationDecl *Spec
    706         = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
    707     const TemplateArgument *Args;
    708     unsigned NumArgs;
    709     if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
    710       const TemplateSpecializationType *TST =
    711         cast<TemplateSpecializationType>(TAW->getType());
    712       Args = TST->getArgs();
    713       NumArgs = TST->getNumArgs();
    714     } else {
    715       const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
    716       Args = TemplateArgs.data();
    717       NumArgs = TemplateArgs.size();
    718     }
    719     IncludeStrongLifetimeRAII Strong(Policy);
    720     Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
    721                                                                     NumArgs,
    722                                                                     Policy);
    723   }
    724 
    725   if (!InnerString.empty()) {
    726     Buffer += ' ';
    727     Buffer += InnerString;
    728   }
    729 
    730   std::swap(Buffer, InnerString);
    731 }
    732 
    733 void TypePrinter::printRecord(const RecordType *T, std::string &S) {
    734   printTag(T->getDecl(), S);
    735 }
    736 
    737 void TypePrinter::printEnum(const EnumType *T, std::string &S) {
    738   printTag(T->getDecl(), S);
    739 }
    740 
    741 void TypePrinter::printTemplateTypeParm(const TemplateTypeParmType *T,
    742                                         std::string &S) {
    743   if (!S.empty())    // Prefix the basic type, e.g. 'parmname X'.
    744     S = ' ' + S;
    745 
    746   if (IdentifierInfo *Id = T->getIdentifier())
    747     S = Id->getName().str() + S;
    748   else
    749     S = "type-parameter-" + llvm::utostr_32(T->getDepth()) + '-' +
    750         llvm::utostr_32(T->getIndex()) + S;
    751 }
    752 
    753 void TypePrinter::printSubstTemplateTypeParm(const SubstTemplateTypeParmType *T,
    754                                              std::string &S) {
    755   IncludeStrongLifetimeRAII Strong(Policy);
    756   print(T->getReplacementType(), S);
    757 }
    758 
    759 void TypePrinter::printSubstTemplateTypeParmPack(
    760                                         const SubstTemplateTypeParmPackType *T,
    761                                              std::string &S) {
    762   IncludeStrongLifetimeRAII Strong(Policy);
    763   printTemplateTypeParm(T->getReplacedParameter(), S);
    764 }
    765 
    766 void TypePrinter::printTemplateSpecialization(
    767                                             const TemplateSpecializationType *T,
    768                                               std::string &S) {
    769   IncludeStrongLifetimeRAII Strong(Policy);
    770   std::string SpecString;
    771 
    772   {
    773     llvm::raw_string_ostream OS(SpecString);
    774     T->getTemplateName().print(OS, Policy);
    775   }
    776 
    777   SpecString += TemplateSpecializationType::PrintTemplateArgumentList(
    778                                                                   T->getArgs(),
    779                                                                 T->getNumArgs(),
    780                                                                       Policy);
    781   if (S.empty())
    782     S.swap(SpecString);
    783   else
    784     S = SpecString + ' ' + S;
    785 }
    786 
    787 void TypePrinter::printInjectedClassName(const InjectedClassNameType *T,
    788                                          std::string &S) {
    789   printTemplateSpecialization(T->getInjectedTST(), S);
    790 }
    791 
    792 void TypePrinter::printElaborated(const ElaboratedType *T, std::string &S) {
    793   std::string MyString;
    794 
    795   {
    796     llvm::raw_string_ostream OS(MyString);
    797     OS << TypeWithKeyword::getKeywordName(T->getKeyword());
    798     if (T->getKeyword() != ETK_None)
    799       OS << " ";
    800     NestedNameSpecifier* Qualifier = T->getQualifier();
    801     if (Qualifier)
    802       Qualifier->print(OS, Policy);
    803   }
    804 
    805   std::string TypeStr;
    806   PrintingPolicy InnerPolicy(Policy);
    807   InnerPolicy.SuppressTagKeyword = true;
    808   InnerPolicy.SuppressScope = true;
    809   TypePrinter(InnerPolicy).print(T->getNamedType(), TypeStr);
    810 
    811   MyString += TypeStr;
    812   if (S.empty())
    813     S.swap(MyString);
    814   else
    815     S = MyString + ' ' + S;
    816 }
    817 
    818 void TypePrinter::printParen(const ParenType *T, std::string &S) {
    819   if (!S.empty() && !isa<FunctionType>(T->getInnerType()))
    820     S = '(' + S + ')';
    821   print(T->getInnerType(), S);
    822 }
    823 
    824 void TypePrinter::printDependentName(const DependentNameType *T, std::string &S) {
    825   std::string MyString;
    826 
    827   {
    828     llvm::raw_string_ostream OS(MyString);
    829     OS << TypeWithKeyword::getKeywordName(T->getKeyword());
    830     if (T->getKeyword() != ETK_None)
    831       OS << " ";
    832 
    833     T->getQualifier()->print(OS, Policy);
    834 
    835     OS << T->getIdentifier()->getName();
    836   }
    837 
    838   if (S.empty())
    839     S.swap(MyString);
    840   else
    841     S = MyString + ' ' + S;
    842 }
    843 
    844 void TypePrinter::printDependentTemplateSpecialization(
    845         const DependentTemplateSpecializationType *T, std::string &S) {
    846   IncludeStrongLifetimeRAII Strong(Policy);
    847   std::string MyString;
    848   {
    849     llvm::raw_string_ostream OS(MyString);
    850 
    851     OS << TypeWithKeyword::getKeywordName(T->getKeyword());
    852     if (T->getKeyword() != ETK_None)
    853       OS << " ";
    854 
    855     if (T->getQualifier())
    856       T->getQualifier()->print(OS, Policy);
    857     OS << T->getIdentifier()->getName();
    858     OS << TemplateSpecializationType::PrintTemplateArgumentList(
    859                                                             T->getArgs(),
    860                                                             T->getNumArgs(),
    861                                                             Policy);
    862   }
    863 
    864   if (S.empty())
    865     S.swap(MyString);
    866   else
    867     S = MyString + ' ' + S;
    868 }
    869 
    870 void TypePrinter::printPackExpansion(const PackExpansionType *T,
    871                                      std::string &S) {
    872   print(T->getPattern(), S);
    873   S += "...";
    874 }
    875 
    876 void TypePrinter::printAttributed(const AttributedType *T,
    877                                   std::string &S) {
    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);
    882 
    883   print(T->getModifiedType(), S);
    884 
    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();
    891     S += ")";
    892     break;
    893 
    894   case AttributedType::attr_vector_size: {
    895     S += "__vector_size__(";
    896     if (const VectorType *vector =T->getEquivalentType()->getAs<VectorType>()) {
    897       S += vector->getNumElements();
    898       S += " * sizeof(";
    899 
    900       std::string tmp;
    901       print(vector->getElementType(), tmp);
    902       S += tmp;
    903       S += ")";
    904     }
    905     S += ")";
    906     break;
    907   }
    908 
    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(";
    913     else
    914       S += "neon_polyvector_type(";
    915     const VectorType *vector = T->getEquivalentType()->getAs<VectorType>();
    916     S += llvm::utostr_32(vector->getNumElements());
    917     S += ")";
    918     break;
    919   }
    920 
    921   case AttributedType::attr_regparm: {
    922     S += "regparm(";
    923     QualType t = T->getEquivalentType();
    924     while (!t->isFunctionType())
    925       t = t->getPointeeType();
    926     S += t->getAs<FunctionType>()->getRegParmType();
    927     S += ")";
    928     break;
    929   }
    930 
    931   case AttributedType::attr_objc_gc: {
    932     S += "objc_gc(";
    933 
    934     QualType tmp = T->getEquivalentType();
    935     while (tmp.getObjCGCAttr() == Qualifiers::GCNone) {
    936       QualType next = tmp->getPointeeType();
    937       if (next == tmp) break;
    938       tmp = next;
    939     }
    940 
    941     if (tmp.isObjCGCWeak())
    942       S += "weak";
    943     else
    944       S += "strong";
    945     S += ")";
    946     break;
    947   }
    948 
    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;
    957     }
    958     S += ")";
    959     break;
    960 
    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: {
    968    S += "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\"");
    974    S += ")";
    975    break;
    976   }
    977   }
    978   S += "))";
    979 }
    980 
    981 void TypePrinter::printObjCInterface(const ObjCInterfaceType *T,
    982                                      std::string &S) {
    983   if (!S.empty())    // Prefix the basic type, e.g. 'typedefname X'.
    984     S = ' ' + S;
    985 
    986   std::string ObjCQIString = T->getDecl()->getNameAsString();
    987   S = ObjCQIString + S;
    988 }
    989 
    990 void TypePrinter::printObjCObject(const ObjCObjectType *T,
    991                                   std::string &S) {
    992   if (T->qual_empty())
    993     return print(T->getBaseType(), S);
    994 
    995   std::string tmp;
    996   print(T->getBaseType(), tmp);
    997   tmp += '<';
    998   bool isFirst = true;
    999   for (ObjCObjectType::qual_iterator
   1000          I = T->qual_begin(), E = T->qual_end(); I != E; ++I) {
   1001     if (isFirst)
   1002       isFirst = false;
   1003     else
   1004       tmp += ',';
   1005     tmp += (*I)->getNameAsString();
   1006   }
   1007   tmp += '>';
   1008 
   1009   if (!S.empty()) {
   1010     tmp += ' ';
   1011     tmp += S;
   1012   }
   1013   std::swap(tmp, S);
   1014 }
   1015 
   1016 void TypePrinter::printObjCObjectPointer(const ObjCObjectPointerType *T,
   1017                                          std::string &S) {
   1018   std::string ObjCQIString;
   1019 
   1020   T->getPointeeType().getLocalQualifiers().getAsStringInternal(ObjCQIString,
   1021                                                                Policy);
   1022   if (!ObjCQIString.empty())
   1023     ObjCQIString += ' ';
   1024 
   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";
   1031   else
   1032     ObjCQIString += T->getInterfaceDecl()->getNameAsString();
   1033 
   1034   if (!T->qual_empty()) {
   1035     ObjCQIString += '<';
   1036     for (ObjCObjectPointerType::qual_iterator I = T->qual_begin(),
   1037                                               E = T->qual_end();
   1038          I != E; ++I) {
   1039       ObjCQIString += (*I)->getNameAsString();
   1040       if (I+1 != E)
   1041         ObjCQIString += ',';
   1042     }
   1043     ObjCQIString += '>';
   1044   }
   1045 
   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'.
   1049     S = ' ' + S;
   1050 
   1051   S = ObjCQIString + S;
   1052 }
   1053 
   1054 std::string TemplateSpecializationType::
   1055   PrintTemplateArgumentList(const TemplateArgumentListInfo &Args,
   1056                             const PrintingPolicy &Policy) {
   1057   return PrintTemplateArgumentList(Args.getArgumentArray(),
   1058                                    Args.size(),
   1059                                    Policy);
   1060 }
   1061 
   1062 std::string
   1063 TemplateSpecializationType::PrintTemplateArgumentList(
   1064                                                 const TemplateArgument *Args,
   1065                                                 unsigned NumArgs,
   1066                                                   const PrintingPolicy &Policy,
   1067                                                       bool SkipBrackets) {
   1068   std::string SpecString;
   1069   if (!SkipBrackets)
   1070     SpecString += '<';
   1071 
   1072   for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
   1073     if (SpecString.size() > unsigned(!SkipBrackets))
   1074       SpecString += ", ";
   1075 
   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(),
   1081                                             Policy, true);
   1082     } else {
   1083       llvm::raw_string_ostream ArgOut(ArgString);
   1084       Args[Arg].print(Policy, ArgOut);
   1085     }
   1086 
   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] == ':')
   1091       SpecString += ' ';
   1092 
   1093     SpecString += ArgString;
   1094   }
   1095 
   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] == '>')
   1100     SpecString += ' ';
   1101 
   1102   if (!SkipBrackets)
   1103     SpecString += '>';
   1104 
   1105   return SpecString;
   1106 }
   1107 
   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;
   1113   SpecString += '<';
   1114   for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
   1115     if (SpecString.size() > 1)
   1116       SpecString += ", ";
   1117 
   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(),
   1124                                             Policy, true);
   1125     } else {
   1126       llvm::raw_string_ostream ArgOut(ArgString);
   1127       Args[Arg].getArgument().print(Policy, ArgOut);
   1128     }
   1129 
   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] == ':')
   1134       SpecString += ' ';
   1135 
   1136     SpecString += ArgString;
   1137   }
   1138 
   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] == '>')
   1143     SpecString += ' ';
   1144 
   1145   SpecString += '>';
   1146 
   1147   return SpecString;
   1148 }
   1149 
   1150 void QualType::dump(const char *msg) const {
   1151   std::string R = "identifier";
   1152   LangOptions LO;
   1153   getAsStringInternal(R, PrintingPolicy(LO));
   1154   if (msg)
   1155     llvm::errs() << msg << ": ";
   1156   llvm::errs() << R << "\n";
   1157 }
   1158 void QualType::dump() const {
   1159   dump("");
   1160 }
   1161 
   1162 void Type::dump() const {
   1163   QualType(this, 0).dump();
   1164 }
   1165 
   1166 std::string Qualifiers::getAsString() const {
   1167   LangOptions LO;
   1168   return getAsString(PrintingPolicy(LO));
   1169 }
   1170 
   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
   1173 // space.
   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:
   1181         S += "__global";
   1182         break;
   1183       case LangAS::opencl_local:
   1184         S += "__local";
   1185         break;
   1186       case LangAS::opencl_constant:
   1187         S += "__constant";
   1188         break;
   1189       default:
   1190         S += "__attribute__((address_space(";
   1191         S += llvm::utostr_32(addrspace);
   1192         S += ")))";
   1193     }
   1194   }
   1195   if (Qualifiers::GC gc = getObjCGCAttr()) {
   1196     if (!S.empty()) S += ' ';
   1197     if (gc == Qualifiers::Weak)
   1198       S += "__weak";
   1199     else
   1200       S += "__strong";
   1201   }
   1202   if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) {
   1203     if (!S.empty() &&
   1204         !(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime))
   1205       S += ' ';
   1206 
   1207     switch (lifetime) {
   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)
   1212         S += "__strong";
   1213       break;
   1214 
   1215     case Qualifiers::OCL_Weak: S += "__weak"; break;
   1216     case Qualifiers::OCL_Autoreleasing: S += "__autoreleasing"; break;
   1217     }
   1218   }
   1219 }
   1220 
   1221 std::string QualType::getAsString(const Type *ty, Qualifiers qs) {
   1222   std::string buffer;
   1223   LangOptions options;
   1224   getAsStringInternal(ty, qs, buffer, PrintingPolicy(options));
   1225   return buffer;
   1226 }
   1227 
   1228 void QualType::getAsStringInternal(const Type *ty, Qualifiers qs,
   1229                                    std::string &buffer,
   1230                                    const PrintingPolicy &policy) {
   1231   TypePrinter(policy).print(ty, qs, buffer);
   1232 }
   1233