clang API Documentation
00001 //===--- TypePrinter.cpp - Pretty-Print Clang Types -----------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This contains code to print types from Clang's type system. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "clang/AST/Decl.h" 00015 #include "clang/AST/DeclObjC.h" 00016 #include "clang/AST/DeclTemplate.h" 00017 #include "clang/AST/Expr.h" 00018 #include "clang/AST/Type.h" 00019 #include "clang/AST/PrettyPrinter.h" 00020 #include "clang/Basic/LangOptions.h" 00021 #include "clang/Basic/SourceManager.h" 00022 #include "llvm/ADT/SmallString.h" 00023 #include "llvm/ADT/StringExtras.h" 00024 #include "llvm/Support/raw_ostream.h" 00025 #include "llvm/Support/SaveAndRestore.h" 00026 using namespace clang; 00027 00028 namespace { 00029 /// \brief RAII object that enables printing of the ARC __strong lifetime 00030 /// qualifier. 00031 class IncludeStrongLifetimeRAII { 00032 PrintingPolicy &Policy; 00033 bool Old; 00034 00035 public: 00036 explicit IncludeStrongLifetimeRAII(PrintingPolicy &Policy) 00037 : Policy(Policy), Old(Policy.SuppressStrongLifetime) { 00038 Policy.SuppressStrongLifetime = false; 00039 } 00040 00041 ~IncludeStrongLifetimeRAII() { 00042 Policy.SuppressStrongLifetime = Old; 00043 } 00044 }; 00045 00046 class ParamPolicyRAII { 00047 PrintingPolicy &Policy; 00048 bool Old; 00049 00050 public: 00051 explicit ParamPolicyRAII(PrintingPolicy &Policy) 00052 : Policy(Policy), Old(Policy.SuppressSpecifiers) { 00053 Policy.SuppressSpecifiers = false; 00054 } 00055 00056 ~ParamPolicyRAII() { 00057 Policy.SuppressSpecifiers = Old; 00058 } 00059 }; 00060 00061 class ElaboratedTypePolicyRAII { 00062 PrintingPolicy &Policy; 00063 bool SuppressTagKeyword; 00064 bool SuppressScope; 00065 00066 public: 00067 explicit ElaboratedTypePolicyRAII(PrintingPolicy &Policy) : Policy(Policy) { 00068 SuppressTagKeyword = Policy.SuppressTagKeyword; 00069 SuppressScope = Policy.SuppressScope; 00070 Policy.SuppressTagKeyword = true; 00071 Policy.SuppressScope = true; 00072 } 00073 00074 ~ElaboratedTypePolicyRAII() { 00075 Policy.SuppressTagKeyword = SuppressTagKeyword; 00076 Policy.SuppressScope = SuppressScope; 00077 } 00078 }; 00079 00080 class TypePrinter { 00081 PrintingPolicy Policy; 00082 bool HasEmptyPlaceHolder; 00083 00084 public: 00085 explicit TypePrinter(const PrintingPolicy &Policy) 00086 : Policy(Policy), HasEmptyPlaceHolder(false) { } 00087 00088 void print(const Type *ty, Qualifiers qs, raw_ostream &OS, 00089 StringRef PlaceHolder); 00090 void print(QualType T, raw_ostream &OS, StringRef PlaceHolder); 00091 00092 static bool canPrefixQualifiers(const Type *T, bool &NeedARCStrongQualifier); 00093 void spaceBeforePlaceHolder(raw_ostream &OS); 00094 void printTypeSpec(const NamedDecl *D, raw_ostream &OS); 00095 00096 void printBefore(const Type *ty, Qualifiers qs, raw_ostream &OS); 00097 void printBefore(QualType T, raw_ostream &OS); 00098 void printAfter(const Type *ty, Qualifiers qs, raw_ostream &OS); 00099 void printAfter(QualType T, raw_ostream &OS); 00100 void AppendScope(DeclContext *DC, raw_ostream &OS); 00101 void printTag(TagDecl *T, raw_ostream &OS); 00102 #define ABSTRACT_TYPE(CLASS, PARENT) 00103 #define TYPE(CLASS, PARENT) \ 00104 void print##CLASS##Before(const CLASS##Type *T, raw_ostream &OS); \ 00105 void print##CLASS##After(const CLASS##Type *T, raw_ostream &OS); 00106 #include "clang/AST/TypeNodes.def" 00107 }; 00108 } 00109 00110 static void AppendTypeQualList(raw_ostream &OS, unsigned TypeQuals) { 00111 bool appendSpace = false; 00112 if (TypeQuals & Qualifiers::Const) { 00113 OS << "const"; 00114 appendSpace = true; 00115 } 00116 if (TypeQuals & Qualifiers::Volatile) { 00117 if (appendSpace) OS << ' '; 00118 OS << "volatile"; 00119 appendSpace = true; 00120 } 00121 if (TypeQuals & Qualifiers::Restrict) { 00122 if (appendSpace) OS << ' '; 00123 OS << "restrict"; 00124 } 00125 } 00126 00127 void TypePrinter::spaceBeforePlaceHolder(raw_ostream &OS) { 00128 if (!HasEmptyPlaceHolder) 00129 OS << ' '; 00130 } 00131 00132 void TypePrinter::print(QualType t, raw_ostream &OS, StringRef PlaceHolder) { 00133 SplitQualType split = t.split(); 00134 print(split.Ty, split.Quals, OS, PlaceHolder); 00135 } 00136 00137 void TypePrinter::print(const Type *T, Qualifiers Quals, raw_ostream &OS, 00138 StringRef PlaceHolder) { 00139 if (!T) { 00140 OS << "NULL TYPE"; 00141 return; 00142 } 00143 00144 if (Policy.SuppressSpecifiers && T->isSpecifierType()) 00145 return; 00146 00147 SaveAndRestore<bool> PHVal(HasEmptyPlaceHolder, PlaceHolder.empty()); 00148 00149 printBefore(T, Quals, OS); 00150 OS << PlaceHolder; 00151 printAfter(T, Quals, OS); 00152 } 00153 00154 bool TypePrinter::canPrefixQualifiers(const Type *T, 00155 bool &NeedARCStrongQualifier) { 00156 // CanPrefixQualifiers - We prefer to print type qualifiers before the type, 00157 // so that we get "const int" instead of "int const", but we can't do this if 00158 // the type is complex. For example if the type is "int*", we *must* print 00159 // "int * const", printing "const int *" is different. Only do this when the 00160 // type expands to a simple string. 00161 bool CanPrefixQualifiers = false; 00162 NeedARCStrongQualifier = false; 00163 Type::TypeClass TC = T->getTypeClass(); 00164 if (const AutoType *AT = dyn_cast<AutoType>(T)) 00165 TC = AT->desugar()->getTypeClass(); 00166 if (const SubstTemplateTypeParmType *Subst 00167 = dyn_cast<SubstTemplateTypeParmType>(T)) 00168 TC = Subst->getReplacementType()->getTypeClass(); 00169 00170 switch (TC) { 00171 case Type::Builtin: 00172 case Type::Complex: 00173 case Type::UnresolvedUsing: 00174 case Type::Typedef: 00175 case Type::TypeOfExpr: 00176 case Type::TypeOf: 00177 case Type::Decltype: 00178 case Type::UnaryTransform: 00179 case Type::Record: 00180 case Type::Enum: 00181 case Type::Elaborated: 00182 case Type::TemplateTypeParm: 00183 case Type::SubstTemplateTypeParmPack: 00184 case Type::TemplateSpecialization: 00185 case Type::InjectedClassName: 00186 case Type::DependentName: 00187 case Type::DependentTemplateSpecialization: 00188 case Type::ObjCObject: 00189 case Type::ObjCInterface: 00190 case Type::Atomic: 00191 CanPrefixQualifiers = true; 00192 break; 00193 00194 case Type::ObjCObjectPointer: 00195 CanPrefixQualifiers = T->isObjCIdType() || T->isObjCClassType() || 00196 T->isObjCQualifiedIdType() || T->isObjCQualifiedClassType(); 00197 break; 00198 00199 case Type::ConstantArray: 00200 case Type::IncompleteArray: 00201 case Type::VariableArray: 00202 case Type::DependentSizedArray: 00203 NeedARCStrongQualifier = true; 00204 // Fall through 00205 00206 case Type::Pointer: 00207 case Type::BlockPointer: 00208 case Type::LValueReference: 00209 case Type::RValueReference: 00210 case Type::MemberPointer: 00211 case Type::DependentSizedExtVector: 00212 case Type::Vector: 00213 case Type::ExtVector: 00214 case Type::FunctionProto: 00215 case Type::FunctionNoProto: 00216 case Type::Paren: 00217 case Type::Attributed: 00218 case Type::PackExpansion: 00219 case Type::SubstTemplateTypeParm: 00220 case Type::Auto: 00221 CanPrefixQualifiers = false; 00222 break; 00223 } 00224 00225 return CanPrefixQualifiers; 00226 } 00227 00228 void TypePrinter::printBefore(QualType t, raw_ostream &OS) { 00229 SplitQualType split = t.split(); 00230 printBefore(split.Ty, split.Quals, OS); 00231 } 00232 00233 /// \brief Prints the part of the type string before an identifier, e.g. for 00234 /// "int foo[10]" it prints "int ". 00235 void TypePrinter::printBefore(const Type *T,Qualifiers Quals, raw_ostream &OS) { 00236 if (Policy.SuppressSpecifiers && T->isSpecifierType()) 00237 return; 00238 00239 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder); 00240 00241 // Print qualifiers as appropriate. 00242 00243 bool CanPrefixQualifiers = false; 00244 bool NeedARCStrongQualifier = false; 00245 CanPrefixQualifiers = canPrefixQualifiers(T, NeedARCStrongQualifier); 00246 00247 if (CanPrefixQualifiers && !Quals.empty()) { 00248 if (NeedARCStrongQualifier) { 00249 IncludeStrongLifetimeRAII Strong(Policy); 00250 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/true); 00251 } else { 00252 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/true); 00253 } 00254 } 00255 00256 bool hasAfterQuals = false; 00257 if (!CanPrefixQualifiers && !Quals.empty()) { 00258 hasAfterQuals = !Quals.isEmptyWhenPrinted(Policy); 00259 if (hasAfterQuals) 00260 HasEmptyPlaceHolder = false; 00261 } 00262 00263 switch (T->getTypeClass()) { 00264 #define ABSTRACT_TYPE(CLASS, PARENT) 00265 #define TYPE(CLASS, PARENT) case Type::CLASS: \ 00266 print##CLASS##Before(cast<CLASS##Type>(T), OS); \ 00267 break; 00268 #include "clang/AST/TypeNodes.def" 00269 } 00270 00271 if (hasAfterQuals) { 00272 if (NeedARCStrongQualifier) { 00273 IncludeStrongLifetimeRAII Strong(Policy); 00274 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/!PrevPHIsEmpty.get()); 00275 } else { 00276 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/!PrevPHIsEmpty.get()); 00277 } 00278 } 00279 } 00280 00281 void TypePrinter::printAfter(QualType t, raw_ostream &OS) { 00282 SplitQualType split = t.split(); 00283 printAfter(split.Ty, split.Quals, OS); 00284 } 00285 00286 /// \brief Prints the part of the type string after an identifier, e.g. for 00287 /// "int foo[10]" it prints "[10]". 00288 void TypePrinter::printAfter(const Type *T, Qualifiers Quals, raw_ostream &OS) { 00289 switch (T->getTypeClass()) { 00290 #define ABSTRACT_TYPE(CLASS, PARENT) 00291 #define TYPE(CLASS, PARENT) case Type::CLASS: \ 00292 print##CLASS##After(cast<CLASS##Type>(T), OS); \ 00293 break; 00294 #include "clang/AST/TypeNodes.def" 00295 } 00296 } 00297 00298 void TypePrinter::printBuiltinBefore(const BuiltinType *T, raw_ostream &OS) { 00299 OS << T->getName(Policy); 00300 spaceBeforePlaceHolder(OS); 00301 } 00302 void TypePrinter::printBuiltinAfter(const BuiltinType *T, raw_ostream &OS) { } 00303 00304 void TypePrinter::printComplexBefore(const ComplexType *T, raw_ostream &OS) { 00305 OS << "_Complex "; 00306 printBefore(T->getElementType(), OS); 00307 } 00308 void TypePrinter::printComplexAfter(const ComplexType *T, raw_ostream &OS) { 00309 printAfter(T->getElementType(), OS); 00310 } 00311 00312 void TypePrinter::printPointerBefore(const PointerType *T, raw_ostream &OS) { 00313 IncludeStrongLifetimeRAII Strong(Policy); 00314 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 00315 printBefore(T->getPointeeType(), OS); 00316 // Handle things like 'int (*A)[4];' correctly. 00317 // FIXME: this should include vectors, but vectors use attributes I guess. 00318 if (isa<ArrayType>(T->getPointeeType())) 00319 OS << '('; 00320 OS << '*'; 00321 } 00322 void TypePrinter::printPointerAfter(const PointerType *T, raw_ostream &OS) { 00323 IncludeStrongLifetimeRAII Strong(Policy); 00324 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 00325 // Handle things like 'int (*A)[4];' correctly. 00326 // FIXME: this should include vectors, but vectors use attributes I guess. 00327 if (isa<ArrayType>(T->getPointeeType())) 00328 OS << ')'; 00329 printAfter(T->getPointeeType(), OS); 00330 } 00331 00332 void TypePrinter::printBlockPointerBefore(const BlockPointerType *T, 00333 raw_ostream &OS) { 00334 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 00335 printBefore(T->getPointeeType(), OS); 00336 OS << '^'; 00337 } 00338 void TypePrinter::printBlockPointerAfter(const BlockPointerType *T, 00339 raw_ostream &OS) { 00340 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 00341 printAfter(T->getPointeeType(), OS); 00342 } 00343 00344 void TypePrinter::printLValueReferenceBefore(const LValueReferenceType *T, 00345 raw_ostream &OS) { 00346 IncludeStrongLifetimeRAII Strong(Policy); 00347 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 00348 printBefore(T->getPointeeTypeAsWritten(), OS); 00349 // Handle things like 'int (&A)[4];' correctly. 00350 // FIXME: this should include vectors, but vectors use attributes I guess. 00351 if (isa<ArrayType>(T->getPointeeTypeAsWritten())) 00352 OS << '('; 00353 OS << '&'; 00354 } 00355 void TypePrinter::printLValueReferenceAfter(const LValueReferenceType *T, 00356 raw_ostream &OS) { 00357 IncludeStrongLifetimeRAII Strong(Policy); 00358 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 00359 // Handle things like 'int (&A)[4];' correctly. 00360 // FIXME: this should include vectors, but vectors use attributes I guess. 00361 if (isa<ArrayType>(T->getPointeeTypeAsWritten())) 00362 OS << ')'; 00363 printAfter(T->getPointeeTypeAsWritten(), OS); 00364 } 00365 00366 void TypePrinter::printRValueReferenceBefore(const RValueReferenceType *T, 00367 raw_ostream &OS) { 00368 IncludeStrongLifetimeRAII Strong(Policy); 00369 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 00370 printBefore(T->getPointeeTypeAsWritten(), OS); 00371 // Handle things like 'int (&&A)[4];' correctly. 00372 // FIXME: this should include vectors, but vectors use attributes I guess. 00373 if (isa<ArrayType>(T->getPointeeTypeAsWritten())) 00374 OS << '('; 00375 OS << "&&"; 00376 } 00377 void TypePrinter::printRValueReferenceAfter(const RValueReferenceType *T, 00378 raw_ostream &OS) { 00379 IncludeStrongLifetimeRAII Strong(Policy); 00380 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 00381 // Handle things like 'int (&&A)[4];' correctly. 00382 // FIXME: this should include vectors, but vectors use attributes I guess. 00383 if (isa<ArrayType>(T->getPointeeTypeAsWritten())) 00384 OS << ')'; 00385 printAfter(T->getPointeeTypeAsWritten(), OS); 00386 } 00387 00388 void TypePrinter::printMemberPointerBefore(const MemberPointerType *T, 00389 raw_ostream &OS) { 00390 IncludeStrongLifetimeRAII Strong(Policy); 00391 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 00392 printBefore(T->getPointeeType(), OS); 00393 // Handle things like 'int (Cls::*A)[4];' correctly. 00394 // FIXME: this should include vectors, but vectors use attributes I guess. 00395 if (isa<ArrayType>(T->getPointeeType())) 00396 OS << '('; 00397 00398 PrintingPolicy InnerPolicy(Policy); 00399 InnerPolicy.SuppressTag = false; 00400 TypePrinter(InnerPolicy).print(QualType(T->getClass(), 0), OS, StringRef()); 00401 00402 OS << "::*"; 00403 } 00404 void TypePrinter::printMemberPointerAfter(const MemberPointerType *T, 00405 raw_ostream &OS) { 00406 IncludeStrongLifetimeRAII Strong(Policy); 00407 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 00408 // Handle things like 'int (Cls::*A)[4];' correctly. 00409 // FIXME: this should include vectors, but vectors use attributes I guess. 00410 if (isa<ArrayType>(T->getPointeeType())) 00411 OS << ')'; 00412 printAfter(T->getPointeeType(), OS); 00413 } 00414 00415 void TypePrinter::printConstantArrayBefore(const ConstantArrayType *T, 00416 raw_ostream &OS) { 00417 IncludeStrongLifetimeRAII Strong(Policy); 00418 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 00419 printBefore(T->getElementType(), OS); 00420 } 00421 void TypePrinter::printConstantArrayAfter(const ConstantArrayType *T, 00422 raw_ostream &OS) { 00423 OS << '[' << T->getSize().getZExtValue() << ']'; 00424 printAfter(T->getElementType(), OS); 00425 } 00426 00427 void TypePrinter::printIncompleteArrayBefore(const IncompleteArrayType *T, 00428 raw_ostream &OS) { 00429 IncludeStrongLifetimeRAII Strong(Policy); 00430 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 00431 printBefore(T->getElementType(), OS); 00432 } 00433 void TypePrinter::printIncompleteArrayAfter(const IncompleteArrayType *T, 00434 raw_ostream &OS) { 00435 OS << "[]"; 00436 printAfter(T->getElementType(), OS); 00437 } 00438 00439 void TypePrinter::printVariableArrayBefore(const VariableArrayType *T, 00440 raw_ostream &OS) { 00441 IncludeStrongLifetimeRAII Strong(Policy); 00442 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 00443 printBefore(T->getElementType(), OS); 00444 } 00445 void TypePrinter::printVariableArrayAfter(const VariableArrayType *T, 00446 raw_ostream &OS) { 00447 OS << '['; 00448 if (T->getIndexTypeQualifiers().hasQualifiers()) { 00449 AppendTypeQualList(OS, T->getIndexTypeCVRQualifiers()); 00450 OS << ' '; 00451 } 00452 00453 if (T->getSizeModifier() == VariableArrayType::Static) 00454 OS << "static"; 00455 else if (T->getSizeModifier() == VariableArrayType::Star) 00456 OS << '*'; 00457 00458 if (T->getSizeExpr()) 00459 T->getSizeExpr()->printPretty(OS, 0, Policy); 00460 OS << ']'; 00461 00462 printAfter(T->getElementType(), OS); 00463 } 00464 00465 void TypePrinter::printDependentSizedArrayBefore( 00466 const DependentSizedArrayType *T, 00467 raw_ostream &OS) { 00468 IncludeStrongLifetimeRAII Strong(Policy); 00469 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 00470 printBefore(T->getElementType(), OS); 00471 } 00472 void TypePrinter::printDependentSizedArrayAfter( 00473 const DependentSizedArrayType *T, 00474 raw_ostream &OS) { 00475 OS << '['; 00476 if (T->getSizeExpr()) 00477 T->getSizeExpr()->printPretty(OS, 0, Policy); 00478 OS << ']'; 00479 printAfter(T->getElementType(), OS); 00480 } 00481 00482 void TypePrinter::printDependentSizedExtVectorBefore( 00483 const DependentSizedExtVectorType *T, 00484 raw_ostream &OS) { 00485 printBefore(T->getElementType(), OS); 00486 } 00487 void TypePrinter::printDependentSizedExtVectorAfter( 00488 const DependentSizedExtVectorType *T, 00489 raw_ostream &OS) { 00490 OS << " __attribute__((ext_vector_type("; 00491 if (T->getSizeExpr()) 00492 T->getSizeExpr()->printPretty(OS, 0, Policy); 00493 OS << ")))"; 00494 printAfter(T->getElementType(), OS); 00495 } 00496 00497 void TypePrinter::printVectorBefore(const VectorType *T, raw_ostream &OS) { 00498 switch (T->getVectorKind()) { 00499 case VectorType::AltiVecPixel: 00500 OS << "__vector __pixel "; 00501 break; 00502 case VectorType::AltiVecBool: 00503 OS << "__vector __bool "; 00504 printBefore(T->getElementType(), OS); 00505 break; 00506 case VectorType::AltiVecVector: 00507 OS << "__vector "; 00508 printBefore(T->getElementType(), OS); 00509 break; 00510 case VectorType::NeonVector: 00511 OS << "__attribute__((neon_vector_type(" 00512 << T->getNumElements() << "))) "; 00513 printBefore(T->getElementType(), OS); 00514 break; 00515 case VectorType::NeonPolyVector: 00516 OS << "__attribute__((neon_polyvector_type(" << 00517 T->getNumElements() << "))) "; 00518 printBefore(T->getElementType(), OS); 00519 break; 00520 case VectorType::GenericVector: { 00521 // FIXME: We prefer to print the size directly here, but have no way 00522 // to get the size of the type. 00523 OS << "__attribute__((__vector_size__(" 00524 << T->getNumElements() 00525 << " * sizeof("; 00526 print(T->getElementType(), OS, StringRef()); 00527 OS << ")))) "; 00528 printBefore(T->getElementType(), OS); 00529 break; 00530 } 00531 } 00532 } 00533 void TypePrinter::printVectorAfter(const VectorType *T, raw_ostream &OS) { 00534 printAfter(T->getElementType(), OS); 00535 } 00536 00537 void TypePrinter::printExtVectorBefore(const ExtVectorType *T, 00538 raw_ostream &OS) { 00539 printBefore(T->getElementType(), OS); 00540 } 00541 void TypePrinter::printExtVectorAfter(const ExtVectorType *T, raw_ostream &OS) { 00542 printAfter(T->getElementType(), OS); 00543 OS << " __attribute__((ext_vector_type("; 00544 OS << T->getNumElements(); 00545 OS << ")))"; 00546 } 00547 00548 void 00549 FunctionProtoType::printExceptionSpecification(raw_ostream &OS, 00550 PrintingPolicy Policy) const { 00551 00552 if (hasDynamicExceptionSpec()) { 00553 OS << " throw("; 00554 if (getExceptionSpecType() == EST_MSAny) 00555 OS << "..."; 00556 else 00557 for (unsigned I = 0, N = getNumExceptions(); I != N; ++I) { 00558 if (I) 00559 OS << ", "; 00560 00561 OS << getExceptionType(I).stream(Policy); 00562 } 00563 OS << ')'; 00564 } else if (isNoexceptExceptionSpec(getExceptionSpecType())) { 00565 OS << " noexcept"; 00566 if (getExceptionSpecType() == EST_ComputedNoexcept) { 00567 OS << '('; 00568 getNoexceptExpr()->printPretty(OS, 0, Policy); 00569 OS << ')'; 00570 } 00571 } 00572 } 00573 00574 void TypePrinter::printFunctionProtoBefore(const FunctionProtoType *T, 00575 raw_ostream &OS) { 00576 if (T->hasTrailingReturn()) { 00577 OS << "auto "; 00578 if (!HasEmptyPlaceHolder) 00579 OS << '('; 00580 } else { 00581 // If needed for precedence reasons, wrap the inner part in grouping parens. 00582 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false); 00583 printBefore(T->getResultType(), OS); 00584 if (!PrevPHIsEmpty.get()) 00585 OS << '('; 00586 } 00587 } 00588 00589 void TypePrinter::printFunctionProtoAfter(const FunctionProtoType *T, 00590 raw_ostream &OS) { 00591 // If needed for precedence reasons, wrap the inner part in grouping parens. 00592 if (!HasEmptyPlaceHolder) 00593 OS << ')'; 00594 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 00595 00596 OS << '('; 00597 { 00598 ParamPolicyRAII ParamPolicy(Policy); 00599 for (unsigned i = 0, e = T->getNumArgs(); i != e; ++i) { 00600 if (i) OS << ", "; 00601 print(T->getArgType(i), OS, StringRef()); 00602 } 00603 } 00604 00605 if (T->isVariadic()) { 00606 if (T->getNumArgs()) 00607 OS << ", "; 00608 OS << "..."; 00609 } else if (T->getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) { 00610 // Do not emit int() if we have a proto, emit 'int(void)'. 00611 OS << "void"; 00612 } 00613 00614 OS << ')'; 00615 00616 FunctionType::ExtInfo Info = T->getExtInfo(); 00617 switch(Info.getCC()) { 00618 case CC_Default: break; 00619 case CC_C: 00620 OS << " __attribute__((cdecl))"; 00621 break; 00622 case CC_X86StdCall: 00623 OS << " __attribute__((stdcall))"; 00624 break; 00625 case CC_X86FastCall: 00626 OS << " __attribute__((fastcall))"; 00627 break; 00628 case CC_X86ThisCall: 00629 OS << " __attribute__((thiscall))"; 00630 break; 00631 case CC_X86Pascal: 00632 OS << " __attribute__((pascal))"; 00633 break; 00634 case CC_AAPCS: 00635 OS << " __attribute__((pcs(\"aapcs\")))"; 00636 break; 00637 case CC_AAPCS_VFP: 00638 OS << " __attribute__((pcs(\"aapcs-vfp\")))"; 00639 break; 00640 } 00641 if (Info.getNoReturn()) 00642 OS << " __attribute__((noreturn))"; 00643 if (Info.getRegParm()) 00644 OS << " __attribute__((regparm (" 00645 << Info.getRegParm() << ")))"; 00646 00647 if (unsigned quals = T->getTypeQuals()) { 00648 OS << ' '; 00649 AppendTypeQualList(OS, quals); 00650 } 00651 00652 switch (T->getRefQualifier()) { 00653 case RQ_None: 00654 break; 00655 00656 case RQ_LValue: 00657 OS << " &"; 00658 break; 00659 00660 case RQ_RValue: 00661 OS << " &&"; 00662 break; 00663 } 00664 T->printExceptionSpecification(OS, Policy); 00665 00666 if (T->hasTrailingReturn()) { 00667 OS << " -> "; 00668 print(T->getResultType(), OS, StringRef()); 00669 } else 00670 printAfter(T->getResultType(), OS); 00671 } 00672 00673 void TypePrinter::printFunctionNoProtoBefore(const FunctionNoProtoType *T, 00674 raw_ostream &OS) { 00675 // If needed for precedence reasons, wrap the inner part in grouping parens. 00676 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false); 00677 printBefore(T->getResultType(), OS); 00678 if (!PrevPHIsEmpty.get()) 00679 OS << '('; 00680 } 00681 void TypePrinter::printFunctionNoProtoAfter(const FunctionNoProtoType *T, 00682 raw_ostream &OS) { 00683 // If needed for precedence reasons, wrap the inner part in grouping parens. 00684 if (!HasEmptyPlaceHolder) 00685 OS << ')'; 00686 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 00687 00688 OS << "()"; 00689 if (T->getNoReturnAttr()) 00690 OS << " __attribute__((noreturn))"; 00691 printAfter(T->getResultType(), OS); 00692 } 00693 00694 void TypePrinter::printTypeSpec(const NamedDecl *D, raw_ostream &OS) { 00695 IdentifierInfo *II = D->getIdentifier(); 00696 OS << II->getName(); 00697 spaceBeforePlaceHolder(OS); 00698 } 00699 00700 void TypePrinter::printUnresolvedUsingBefore(const UnresolvedUsingType *T, 00701 raw_ostream &OS) { 00702 printTypeSpec(T->getDecl(), OS); 00703 } 00704 void TypePrinter::printUnresolvedUsingAfter(const UnresolvedUsingType *T, 00705 raw_ostream &OS) { } 00706 00707 void TypePrinter::printTypedefBefore(const TypedefType *T, raw_ostream &OS) { 00708 printTypeSpec(T->getDecl(), OS); 00709 } 00710 void TypePrinter::printTypedefAfter(const TypedefType *T, raw_ostream &OS) { } 00711 00712 void TypePrinter::printTypeOfExprBefore(const TypeOfExprType *T, 00713 raw_ostream &OS) { 00714 OS << "typeof "; 00715 T->getUnderlyingExpr()->printPretty(OS, 0, Policy); 00716 spaceBeforePlaceHolder(OS); 00717 } 00718 void TypePrinter::printTypeOfExprAfter(const TypeOfExprType *T, 00719 raw_ostream &OS) { } 00720 00721 void TypePrinter::printTypeOfBefore(const TypeOfType *T, raw_ostream &OS) { 00722 OS << "typeof("; 00723 print(T->getUnderlyingType(), OS, StringRef()); 00724 OS << ')'; 00725 spaceBeforePlaceHolder(OS); 00726 } 00727 void TypePrinter::printTypeOfAfter(const TypeOfType *T, raw_ostream &OS) { } 00728 00729 void TypePrinter::printDecltypeBefore(const DecltypeType *T, raw_ostream &OS) { 00730 OS << "decltype("; 00731 T->getUnderlyingExpr()->printPretty(OS, 0, Policy); 00732 OS << ')'; 00733 spaceBeforePlaceHolder(OS); 00734 } 00735 void TypePrinter::printDecltypeAfter(const DecltypeType *T, raw_ostream &OS) { } 00736 00737 void TypePrinter::printUnaryTransformBefore(const UnaryTransformType *T, 00738 raw_ostream &OS) { 00739 IncludeStrongLifetimeRAII Strong(Policy); 00740 00741 switch (T->getUTTKind()) { 00742 case UnaryTransformType::EnumUnderlyingType: 00743 OS << "__underlying_type("; 00744 print(T->getBaseType(), OS, StringRef()); 00745 OS << ')'; 00746 spaceBeforePlaceHolder(OS); 00747 return; 00748 } 00749 00750 printBefore(T->getBaseType(), OS); 00751 } 00752 void TypePrinter::printUnaryTransformAfter(const UnaryTransformType *T, 00753 raw_ostream &OS) { 00754 IncludeStrongLifetimeRAII Strong(Policy); 00755 00756 switch (T->getUTTKind()) { 00757 case UnaryTransformType::EnumUnderlyingType: 00758 return; 00759 } 00760 00761 printAfter(T->getBaseType(), OS); 00762 } 00763 00764 void TypePrinter::printAutoBefore(const AutoType *T, raw_ostream &OS) { 00765 // If the type has been deduced, do not print 'auto'. 00766 if (T->isDeduced()) { 00767 printBefore(T->getDeducedType(), OS); 00768 } else { 00769 OS << "auto"; 00770 spaceBeforePlaceHolder(OS); 00771 } 00772 } 00773 void TypePrinter::printAutoAfter(const AutoType *T, raw_ostream &OS) { 00774 // If the type has been deduced, do not print 'auto'. 00775 if (T->isDeduced()) 00776 printAfter(T->getDeducedType(), OS); 00777 } 00778 00779 void TypePrinter::printAtomicBefore(const AtomicType *T, raw_ostream &OS) { 00780 IncludeStrongLifetimeRAII Strong(Policy); 00781 00782 OS << "_Atomic("; 00783 print(T->getValueType(), OS, StringRef()); 00784 OS << ')'; 00785 spaceBeforePlaceHolder(OS); 00786 } 00787 void TypePrinter::printAtomicAfter(const AtomicType *T, raw_ostream &OS) { } 00788 00789 /// Appends the given scope to the end of a string. 00790 void TypePrinter::AppendScope(DeclContext *DC, raw_ostream &OS) { 00791 if (DC->isTranslationUnit()) return; 00792 AppendScope(DC->getParent(), OS); 00793 00794 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) { 00795 if (Policy.SuppressUnwrittenScope && 00796 (NS->isAnonymousNamespace() || NS->isInline())) 00797 return; 00798 if (NS->getIdentifier()) 00799 OS << NS->getName() << "::"; 00800 else 00801 OS << "<anonymous>::"; 00802 } else if (ClassTemplateSpecializationDecl *Spec 00803 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) { 00804 IncludeStrongLifetimeRAII Strong(Policy); 00805 OS << Spec->getIdentifier()->getName(); 00806 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 00807 TemplateSpecializationType::PrintTemplateArgumentList(OS, 00808 TemplateArgs.data(), 00809 TemplateArgs.size(), 00810 Policy); 00811 OS << "::"; 00812 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) { 00813 if (TypedefNameDecl *Typedef = Tag->getTypedefNameForAnonDecl()) 00814 OS << Typedef->getIdentifier()->getName() << "::"; 00815 else if (Tag->getIdentifier()) 00816 OS << Tag->getIdentifier()->getName() << "::"; 00817 else 00818 return; 00819 } 00820 } 00821 00822 void TypePrinter::printTag(TagDecl *D, raw_ostream &OS) { 00823 if (Policy.SuppressTag) 00824 return; 00825 00826 bool HasKindDecoration = false; 00827 00828 // bool SuppressTagKeyword 00829 // = Policy.LangOpts.CPlusPlus || Policy.SuppressTagKeyword; 00830 00831 // We don't print tags unless this is an elaborated type. 00832 // In C, we just assume every RecordType is an elaborated type. 00833 if (!(Policy.LangOpts.CPlusPlus || Policy.SuppressTagKeyword || 00834 D->getTypedefNameForAnonDecl())) { 00835 HasKindDecoration = true; 00836 OS << D->getKindName(); 00837 OS << ' '; 00838 } 00839 00840 // Compute the full nested-name-specifier for this type. 00841 // In C, this will always be empty except when the type 00842 // being printed is anonymous within other Record. 00843 if (!Policy.SuppressScope) 00844 AppendScope(D->getDeclContext(), OS); 00845 00846 if (const IdentifierInfo *II = D->getIdentifier()) 00847 OS << II->getName(); 00848 else if (TypedefNameDecl *Typedef = D->getTypedefNameForAnonDecl()) { 00849 assert(Typedef->getIdentifier() && "Typedef without identifier?"); 00850 OS << Typedef->getIdentifier()->getName(); 00851 } else { 00852 // Make an unambiguous representation for anonymous types, e.g. 00853 // <anonymous enum at /usr/include/string.h:120:9> 00854 00855 if (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda()) { 00856 OS << "<lambda"; 00857 HasKindDecoration = true; 00858 } else { 00859 OS << "<anonymous"; 00860 } 00861 00862 if (Policy.AnonymousTagLocations) { 00863 // Suppress the redundant tag keyword if we just printed one. 00864 // We don't have to worry about ElaboratedTypes here because you can't 00865 // refer to an anonymous type with one. 00866 if (!HasKindDecoration) 00867 OS << " " << D->getKindName(); 00868 00869 PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc( 00870 D->getLocation()); 00871 if (PLoc.isValid()) { 00872 OS << " at " << PLoc.getFilename() 00873 << ':' << PLoc.getLine() 00874 << ':' << PLoc.getColumn(); 00875 } 00876 } 00877 00878 OS << '>'; 00879 } 00880 00881 // If this is a class template specialization, print the template 00882 // arguments. 00883 if (ClassTemplateSpecializationDecl *Spec 00884 = dyn_cast<ClassTemplateSpecializationDecl>(D)) { 00885 const TemplateArgument *Args; 00886 unsigned NumArgs; 00887 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) { 00888 const TemplateSpecializationType *TST = 00889 cast<TemplateSpecializationType>(TAW->getType()); 00890 Args = TST->getArgs(); 00891 NumArgs = TST->getNumArgs(); 00892 } else { 00893 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 00894 Args = TemplateArgs.data(); 00895 NumArgs = TemplateArgs.size(); 00896 } 00897 IncludeStrongLifetimeRAII Strong(Policy); 00898 TemplateSpecializationType::PrintTemplateArgumentList(OS, 00899 Args, NumArgs, 00900 Policy); 00901 } 00902 00903 spaceBeforePlaceHolder(OS); 00904 } 00905 00906 void TypePrinter::printRecordBefore(const RecordType *T, raw_ostream &OS) { 00907 printTag(T->getDecl(), OS); 00908 } 00909 void TypePrinter::printRecordAfter(const RecordType *T, raw_ostream &OS) { } 00910 00911 void TypePrinter::printEnumBefore(const EnumType *T, raw_ostream &OS) { 00912 printTag(T->getDecl(), OS); 00913 } 00914 void TypePrinter::printEnumAfter(const EnumType *T, raw_ostream &OS) { } 00915 00916 void TypePrinter::printTemplateTypeParmBefore(const TemplateTypeParmType *T, 00917 raw_ostream &OS) { 00918 if (IdentifierInfo *Id = T->getIdentifier()) 00919 OS << Id->getName(); 00920 else 00921 OS << "type-parameter-" << T->getDepth() << '-' << T->getIndex(); 00922 spaceBeforePlaceHolder(OS); 00923 } 00924 void TypePrinter::printTemplateTypeParmAfter(const TemplateTypeParmType *T, 00925 raw_ostream &OS) { } 00926 00927 void TypePrinter::printSubstTemplateTypeParmBefore( 00928 const SubstTemplateTypeParmType *T, 00929 raw_ostream &OS) { 00930 IncludeStrongLifetimeRAII Strong(Policy); 00931 printBefore(T->getReplacementType(), OS); 00932 } 00933 void TypePrinter::printSubstTemplateTypeParmAfter( 00934 const SubstTemplateTypeParmType *T, 00935 raw_ostream &OS) { 00936 IncludeStrongLifetimeRAII Strong(Policy); 00937 printAfter(T->getReplacementType(), OS); 00938 } 00939 00940 void TypePrinter::printSubstTemplateTypeParmPackBefore( 00941 const SubstTemplateTypeParmPackType *T, 00942 raw_ostream &OS) { 00943 IncludeStrongLifetimeRAII Strong(Policy); 00944 printTemplateTypeParmBefore(T->getReplacedParameter(), OS); 00945 } 00946 void TypePrinter::printSubstTemplateTypeParmPackAfter( 00947 const SubstTemplateTypeParmPackType *T, 00948 raw_ostream &OS) { 00949 IncludeStrongLifetimeRAII Strong(Policy); 00950 printTemplateTypeParmAfter(T->getReplacedParameter(), OS); 00951 } 00952 00953 void TypePrinter::printTemplateSpecializationBefore( 00954 const TemplateSpecializationType *T, 00955 raw_ostream &OS) { 00956 IncludeStrongLifetimeRAII Strong(Policy); 00957 T->getTemplateName().print(OS, Policy); 00958 00959 TemplateSpecializationType::PrintTemplateArgumentList(OS, 00960 T->getArgs(), 00961 T->getNumArgs(), 00962 Policy); 00963 spaceBeforePlaceHolder(OS); 00964 } 00965 void TypePrinter::printTemplateSpecializationAfter( 00966 const TemplateSpecializationType *T, 00967 raw_ostream &OS) { } 00968 00969 void TypePrinter::printInjectedClassNameBefore(const InjectedClassNameType *T, 00970 raw_ostream &OS) { 00971 printTemplateSpecializationBefore(T->getInjectedTST(), OS); 00972 } 00973 void TypePrinter::printInjectedClassNameAfter(const InjectedClassNameType *T, 00974 raw_ostream &OS) { } 00975 00976 void TypePrinter::printElaboratedBefore(const ElaboratedType *T, 00977 raw_ostream &OS) { 00978 OS << TypeWithKeyword::getKeywordName(T->getKeyword()); 00979 if (T->getKeyword() != ETK_None) 00980 OS << " "; 00981 NestedNameSpecifier* Qualifier = T->getQualifier(); 00982 if (Qualifier) 00983 Qualifier->print(OS, Policy); 00984 00985 ElaboratedTypePolicyRAII PolicyRAII(Policy); 00986 printBefore(T->getNamedType(), OS); 00987 } 00988 void TypePrinter::printElaboratedAfter(const ElaboratedType *T, 00989 raw_ostream &OS) { 00990 ElaboratedTypePolicyRAII PolicyRAII(Policy); 00991 printAfter(T->getNamedType(), OS); 00992 } 00993 00994 void TypePrinter::printParenBefore(const ParenType *T, raw_ostream &OS) { 00995 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) { 00996 printBefore(T->getInnerType(), OS); 00997 OS << '('; 00998 } else 00999 printBefore(T->getInnerType(), OS); 01000 } 01001 void TypePrinter::printParenAfter(const ParenType *T, raw_ostream &OS) { 01002 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) { 01003 OS << ')'; 01004 printAfter(T->getInnerType(), OS); 01005 } else 01006 printAfter(T->getInnerType(), OS); 01007 } 01008 01009 void TypePrinter::printDependentNameBefore(const DependentNameType *T, 01010 raw_ostream &OS) { 01011 OS << TypeWithKeyword::getKeywordName(T->getKeyword()); 01012 if (T->getKeyword() != ETK_None) 01013 OS << " "; 01014 01015 T->getQualifier()->print(OS, Policy); 01016 01017 OS << T->getIdentifier()->getName(); 01018 spaceBeforePlaceHolder(OS); 01019 } 01020 void TypePrinter::printDependentNameAfter(const DependentNameType *T, 01021 raw_ostream &OS) { } 01022 01023 void TypePrinter::printDependentTemplateSpecializationBefore( 01024 const DependentTemplateSpecializationType *T, raw_ostream &OS) { 01025 IncludeStrongLifetimeRAII Strong(Policy); 01026 01027 OS << TypeWithKeyword::getKeywordName(T->getKeyword()); 01028 if (T->getKeyword() != ETK_None) 01029 OS << " "; 01030 01031 if (T->getQualifier()) 01032 T->getQualifier()->print(OS, Policy); 01033 OS << T->getIdentifier()->getName(); 01034 TemplateSpecializationType::PrintTemplateArgumentList(OS, 01035 T->getArgs(), 01036 T->getNumArgs(), 01037 Policy); 01038 spaceBeforePlaceHolder(OS); 01039 } 01040 void TypePrinter::printDependentTemplateSpecializationAfter( 01041 const DependentTemplateSpecializationType *T, raw_ostream &OS) { } 01042 01043 void TypePrinter::printPackExpansionBefore(const PackExpansionType *T, 01044 raw_ostream &OS) { 01045 printBefore(T->getPattern(), OS); 01046 } 01047 void TypePrinter::printPackExpansionAfter(const PackExpansionType *T, 01048 raw_ostream &OS) { 01049 printAfter(T->getPattern(), OS); 01050 OS << "..."; 01051 } 01052 01053 void TypePrinter::printAttributedBefore(const AttributedType *T, 01054 raw_ostream &OS) { 01055 // Prefer the macro forms of the GC and ownership qualifiers. 01056 if (T->getAttrKind() == AttributedType::attr_objc_gc || 01057 T->getAttrKind() == AttributedType::attr_objc_ownership) 01058 return printBefore(T->getEquivalentType(), OS); 01059 01060 printBefore(T->getModifiedType(), OS); 01061 } 01062 01063 void TypePrinter::printAttributedAfter(const AttributedType *T, 01064 raw_ostream &OS) { 01065 // Prefer the macro forms of the GC and ownership qualifiers. 01066 if (T->getAttrKind() == AttributedType::attr_objc_gc || 01067 T->getAttrKind() == AttributedType::attr_objc_ownership) 01068 return printAfter(T->getEquivalentType(), OS); 01069 01070 // TODO: not all attributes are GCC-style attributes. 01071 OS << " __attribute__(("; 01072 switch (T->getAttrKind()) { 01073 case AttributedType::attr_address_space: 01074 OS << "address_space("; 01075 OS << T->getEquivalentType().getAddressSpace(); 01076 OS << ')'; 01077 break; 01078 01079 case AttributedType::attr_vector_size: { 01080 OS << "__vector_size__("; 01081 if (const VectorType *vector =T->getEquivalentType()->getAs<VectorType>()) { 01082 OS << vector->getNumElements(); 01083 OS << " * sizeof("; 01084 print(vector->getElementType(), OS, StringRef()); 01085 OS << ')'; 01086 } 01087 OS << ')'; 01088 break; 01089 } 01090 01091 case AttributedType::attr_neon_vector_type: 01092 case AttributedType::attr_neon_polyvector_type: { 01093 if (T->getAttrKind() == AttributedType::attr_neon_vector_type) 01094 OS << "neon_vector_type("; 01095 else 01096 OS << "neon_polyvector_type("; 01097 const VectorType *vector = T->getEquivalentType()->getAs<VectorType>(); 01098 OS << vector->getNumElements(); 01099 OS << ')'; 01100 break; 01101 } 01102 01103 case AttributedType::attr_regparm: { 01104 OS << "regparm("; 01105 QualType t = T->getEquivalentType(); 01106 while (!t->isFunctionType()) 01107 t = t->getPointeeType(); 01108 OS << t->getAs<FunctionType>()->getRegParmType(); 01109 OS << ')'; 01110 break; 01111 } 01112 01113 case AttributedType::attr_objc_gc: { 01114 OS << "objc_gc("; 01115 01116 QualType tmp = T->getEquivalentType(); 01117 while (tmp.getObjCGCAttr() == Qualifiers::GCNone) { 01118 QualType next = tmp->getPointeeType(); 01119 if (next == tmp) break; 01120 tmp = next; 01121 } 01122 01123 if (tmp.isObjCGCWeak()) 01124 OS << "weak"; 01125 else 01126 OS << "strong"; 01127 OS << ')'; 01128 break; 01129 } 01130 01131 case AttributedType::attr_objc_ownership: 01132 OS << "objc_ownership("; 01133 switch (T->getEquivalentType().getObjCLifetime()) { 01134 case Qualifiers::OCL_None: llvm_unreachable("no ownership!"); 01135 case Qualifiers::OCL_ExplicitNone: OS << "none"; break; 01136 case Qualifiers::OCL_Strong: OS << "strong"; break; 01137 case Qualifiers::OCL_Weak: OS << "weak"; break; 01138 case Qualifiers::OCL_Autoreleasing: OS << "autoreleasing"; break; 01139 } 01140 OS << ')'; 01141 break; 01142 01143 case AttributedType::attr_noreturn: OS << "noreturn"; break; 01144 case AttributedType::attr_cdecl: OS << "cdecl"; break; 01145 case AttributedType::attr_fastcall: OS << "fastcall"; break; 01146 case AttributedType::attr_stdcall: OS << "stdcall"; break; 01147 case AttributedType::attr_thiscall: OS << "thiscall"; break; 01148 case AttributedType::attr_pascal: OS << "pascal"; break; 01149 case AttributedType::attr_pcs: { 01150 OS << "pcs("; 01151 QualType t = T->getEquivalentType(); 01152 while (!t->isFunctionType()) 01153 t = t->getPointeeType(); 01154 OS << (t->getAs<FunctionType>()->getCallConv() == CC_AAPCS ? 01155 "\"aapcs\"" : "\"aapcs-vfp\""); 01156 OS << ')'; 01157 break; 01158 } 01159 } 01160 OS << "))"; 01161 } 01162 01163 void TypePrinter::printObjCInterfaceBefore(const ObjCInterfaceType *T, 01164 raw_ostream &OS) { 01165 OS << T->getDecl()->getName(); 01166 spaceBeforePlaceHolder(OS); 01167 } 01168 void TypePrinter::printObjCInterfaceAfter(const ObjCInterfaceType *T, 01169 raw_ostream &OS) { } 01170 01171 void TypePrinter::printObjCObjectBefore(const ObjCObjectType *T, 01172 raw_ostream &OS) { 01173 if (T->qual_empty()) 01174 return printBefore(T->getBaseType(), OS); 01175 01176 print(T->getBaseType(), OS, StringRef()); 01177 OS << '<'; 01178 bool isFirst = true; 01179 for (ObjCObjectType::qual_iterator 01180 I = T->qual_begin(), E = T->qual_end(); I != E; ++I) { 01181 if (isFirst) 01182 isFirst = false; 01183 else 01184 OS << ','; 01185 OS << (*I)->getName(); 01186 } 01187 OS << '>'; 01188 spaceBeforePlaceHolder(OS); 01189 } 01190 void TypePrinter::printObjCObjectAfter(const ObjCObjectType *T, 01191 raw_ostream &OS) { 01192 if (T->qual_empty()) 01193 return printAfter(T->getBaseType(), OS); 01194 } 01195 01196 void TypePrinter::printObjCObjectPointerBefore(const ObjCObjectPointerType *T, 01197 raw_ostream &OS) { 01198 T->getPointeeType().getLocalQualifiers().print(OS, Policy, 01199 /*appendSpaceIfNonEmpty=*/true); 01200 01201 if (T->isObjCIdType() || T->isObjCQualifiedIdType()) 01202 OS << "id"; 01203 else if (T->isObjCClassType() || T->isObjCQualifiedClassType()) 01204 OS << "Class"; 01205 else if (T->isObjCSelType()) 01206 OS << "SEL"; 01207 else 01208 OS << T->getInterfaceDecl()->getName(); 01209 01210 if (!T->qual_empty()) { 01211 OS << '<'; 01212 for (ObjCObjectPointerType::qual_iterator I = T->qual_begin(), 01213 E = T->qual_end(); 01214 I != E; ++I) { 01215 OS << (*I)->getName(); 01216 if (I+1 != E) 01217 OS << ','; 01218 } 01219 OS << '>'; 01220 } 01221 01222 if (!T->isObjCIdType() && !T->isObjCQualifiedIdType()) { 01223 OS << " *"; // Don't forget the implicit pointer. 01224 } else { 01225 spaceBeforePlaceHolder(OS); 01226 } 01227 } 01228 void TypePrinter::printObjCObjectPointerAfter(const ObjCObjectPointerType *T, 01229 raw_ostream &OS) { } 01230 01231 void TemplateSpecializationType:: 01232 PrintTemplateArgumentList(raw_ostream &OS, 01233 const TemplateArgumentListInfo &Args, 01234 const PrintingPolicy &Policy) { 01235 return PrintTemplateArgumentList(OS, 01236 Args.getArgumentArray(), 01237 Args.size(), 01238 Policy); 01239 } 01240 01241 void 01242 TemplateSpecializationType::PrintTemplateArgumentList( 01243 raw_ostream &OS, 01244 const TemplateArgument *Args, 01245 unsigned NumArgs, 01246 const PrintingPolicy &Policy, 01247 bool SkipBrackets) { 01248 if (!SkipBrackets) 01249 OS << '<'; 01250 01251 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { 01252 if (Arg > 0) 01253 OS << ", "; 01254 01255 // Print the argument into a string. 01256 SmallString<128> Buf; 01257 llvm::raw_svector_ostream ArgOS(Buf); 01258 if (Args[Arg].getKind() == TemplateArgument::Pack) { 01259 PrintTemplateArgumentList(ArgOS, 01260 Args[Arg].pack_begin(), 01261 Args[Arg].pack_size(), 01262 Policy, true); 01263 } else { 01264 Args[Arg].print(Policy, ArgOS); 01265 } 01266 StringRef ArgString = ArgOS.str(); 01267 01268 // If this is the first argument and its string representation 01269 // begins with the global scope specifier ('::foo'), add a space 01270 // to avoid printing the diagraph '<:'. 01271 if (!Arg && !ArgString.empty() && ArgString[0] == ':') 01272 OS << ' '; 01273 01274 OS << ArgString; 01275 } 01276 01277 if (!SkipBrackets) 01278 OS << '>'; 01279 } 01280 01281 // Sadly, repeat all that with TemplateArgLoc. 01282 void TemplateSpecializationType:: 01283 PrintTemplateArgumentList(raw_ostream &OS, 01284 const TemplateArgumentLoc *Args, unsigned NumArgs, 01285 const PrintingPolicy &Policy) { 01286 OS << '<'; 01287 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { 01288 if (Arg > 0) 01289 OS << ", "; 01290 01291 // Print the argument into a string. 01292 SmallString<128> Buf; 01293 llvm::raw_svector_ostream ArgOS(Buf); 01294 if (Args[Arg].getArgument().getKind() == TemplateArgument::Pack) { 01295 PrintTemplateArgumentList(ArgOS, 01296 Args[Arg].getArgument().pack_begin(), 01297 Args[Arg].getArgument().pack_size(), 01298 Policy, true); 01299 } else { 01300 Args[Arg].getArgument().print(Policy, ArgOS); 01301 } 01302 StringRef ArgString = ArgOS.str(); 01303 01304 // If this is the first argument and its string representation 01305 // begins with the global scope specifier ('::foo'), add a space 01306 // to avoid printing the diagraph '<:'. 01307 if (!Arg && !ArgString.empty() && ArgString[0] == ':') 01308 OS << ' '; 01309 01310 OS << ArgString; 01311 } 01312 01313 OS << '>'; 01314 } 01315 01316 void 01317 FunctionProtoType::printExceptionSpecification(std::string &S, 01318 PrintingPolicy Policy) const { 01319 01320 if (hasDynamicExceptionSpec()) { 01321 S += " throw("; 01322 if (getExceptionSpecType() == EST_MSAny) 01323 S += "..."; 01324 else 01325 for (unsigned I = 0, N = getNumExceptions(); I != N; ++I) { 01326 if (I) 01327 S += ", "; 01328 01329 S += getExceptionType(I).getAsString(Policy); 01330 } 01331 S += ")"; 01332 } else if (isNoexceptExceptionSpec(getExceptionSpecType())) { 01333 S += " noexcept"; 01334 if (getExceptionSpecType() == EST_ComputedNoexcept) { 01335 S += "("; 01336 llvm::raw_string_ostream EOut(S); 01337 getNoexceptExpr()->printPretty(EOut, 0, Policy); 01338 EOut.flush(); 01339 S += EOut.str(); 01340 S += ")"; 01341 } 01342 } 01343 } 01344 01345 std::string TemplateSpecializationType:: 01346 PrintTemplateArgumentList(const TemplateArgumentListInfo &Args, 01347 const PrintingPolicy &Policy) { 01348 return PrintTemplateArgumentList(Args.getArgumentArray(), 01349 Args.size(), 01350 Policy); 01351 } 01352 01353 std::string 01354 TemplateSpecializationType::PrintTemplateArgumentList( 01355 const TemplateArgument *Args, 01356 unsigned NumArgs, 01357 const PrintingPolicy &Policy, 01358 bool SkipBrackets) { 01359 std::string SpecString; 01360 if (!SkipBrackets) 01361 SpecString += '<'; 01362 01363 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { 01364 if (SpecString.size() > unsigned(!SkipBrackets)) 01365 SpecString += ", "; 01366 01367 // Print the argument into a string. 01368 std::string ArgString; 01369 if (Args[Arg].getKind() == TemplateArgument::Pack) { 01370 ArgString = PrintTemplateArgumentList(Args[Arg].pack_begin(), 01371 Args[Arg].pack_size(), 01372 Policy, true); 01373 } else { 01374 llvm::raw_string_ostream ArgOut(ArgString); 01375 Args[Arg].print(Policy, ArgOut); 01376 } 01377 01378 // If this is the first argument and its string representation 01379 // begins with the global scope specifier ('::foo'), add a space 01380 // to avoid printing the diagraph '<:'. 01381 if (!Arg && !ArgString.empty() && ArgString[0] == ':') 01382 SpecString += ' '; 01383 01384 SpecString += ArgString; 01385 } 01386 01387 // If the last character of our string is '>', add another space to 01388 // keep the two '>''s separate tokens. We don't *have* to do this in 01389 // C++0x, but it's still good hygiene. 01390 if (!SpecString.empty() && SpecString[SpecString.size() - 1] == '>') 01391 SpecString += ' '; 01392 01393 if (!SkipBrackets) 01394 SpecString += '>'; 01395 01396 return SpecString; 01397 } 01398 01399 // Sadly, repeat all that with TemplateArgLoc. 01400 std::string TemplateSpecializationType:: 01401 PrintTemplateArgumentList(const TemplateArgumentLoc *Args, unsigned NumArgs, 01402 const PrintingPolicy &Policy) { 01403 std::string SpecString; 01404 SpecString += '<'; 01405 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { 01406 if (SpecString.size() > 1) 01407 SpecString += ", "; 01408 01409 // Print the argument into a string. 01410 std::string ArgString; 01411 if (Args[Arg].getArgument().getKind() == TemplateArgument::Pack) { 01412 ArgString = PrintTemplateArgumentList( 01413 Args[Arg].getArgument().pack_begin(), 01414 Args[Arg].getArgument().pack_size(), 01415 Policy, true); 01416 } else { 01417 llvm::raw_string_ostream ArgOut(ArgString); 01418 Args[Arg].getArgument().print(Policy, ArgOut); 01419 } 01420 01421 // If this is the first argument and its string representation 01422 // begins with the global scope specifier ('::foo'), add a space 01423 // to avoid printing the diagraph '<:'. 01424 if (!Arg && !ArgString.empty() && ArgString[0] == ':') 01425 SpecString += ' '; 01426 01427 SpecString += ArgString; 01428 } 01429 01430 // If the last character of our string is '>', add another space to 01431 // keep the two '>''s separate tokens. We don't *have* to do this in 01432 // C++0x, but it's still good hygiene. 01433 if (SpecString[SpecString.size() - 1] == '>') 01434 SpecString += ' '; 01435 01436 SpecString += '>'; 01437 01438 return SpecString; 01439 } 01440 01441 void QualType::dump(const char *msg) const { 01442 if (msg) 01443 llvm::errs() << msg << ": "; 01444 LangOptions LO; 01445 print(llvm::errs(), PrintingPolicy(LO), "identifier"); 01446 llvm::errs() << '\n'; 01447 } 01448 void QualType::dump() const { 01449 dump(0); 01450 } 01451 01452 void Type::dump() const { 01453 QualType(this, 0).dump(); 01454 } 01455 01456 std::string Qualifiers::getAsString() const { 01457 LangOptions LO; 01458 return getAsString(PrintingPolicy(LO)); 01459 } 01460 01461 // Appends qualifiers to the given string, separated by spaces. Will 01462 // prefix a space if the string is non-empty. Will not append a final 01463 // space. 01464 std::string Qualifiers::getAsString(const PrintingPolicy &Policy) const { 01465 SmallString<64> Buf; 01466 llvm::raw_svector_ostream StrOS(Buf); 01467 print(StrOS, Policy); 01468 return StrOS.str(); 01469 } 01470 01471 bool Qualifiers::isEmptyWhenPrinted(const PrintingPolicy &Policy) const { 01472 if (getCVRQualifiers()) 01473 return false; 01474 01475 if (getAddressSpace()) 01476 return false; 01477 01478 if (getObjCGCAttr()) 01479 return false; 01480 01481 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) 01482 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime)) 01483 return false; 01484 01485 return true; 01486 } 01487 01488 // Appends qualifiers to the given string, separated by spaces. Will 01489 // prefix a space if the string is non-empty. Will not append a final 01490 // space. 01491 void Qualifiers::print(raw_ostream &OS, const PrintingPolicy& Policy, 01492 bool appendSpaceIfNonEmpty) const { 01493 bool addSpace = false; 01494 01495 unsigned quals = getCVRQualifiers(); 01496 if (quals) { 01497 AppendTypeQualList(OS, quals); 01498 addSpace = true; 01499 } 01500 if (unsigned addrspace = getAddressSpace()) { 01501 if (addSpace) 01502 OS << ' '; 01503 addSpace = true; 01504 switch (addrspace) { 01505 case LangAS::opencl_global: 01506 OS << "__global"; 01507 break; 01508 case LangAS::opencl_local: 01509 OS << "__local"; 01510 break; 01511 case LangAS::opencl_constant: 01512 OS << "__constant"; 01513 break; 01514 default: 01515 OS << "__attribute__((address_space("; 01516 OS << addrspace; 01517 OS << ")))"; 01518 } 01519 } 01520 if (Qualifiers::GC gc = getObjCGCAttr()) { 01521 if (addSpace) 01522 OS << ' '; 01523 addSpace = true; 01524 if (gc == Qualifiers::Weak) 01525 OS << "__weak"; 01526 else 01527 OS << "__strong"; 01528 } 01529 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) { 01530 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime)){ 01531 if (addSpace) 01532 OS << ' '; 01533 addSpace = true; 01534 } 01535 01536 switch (lifetime) { 01537 case Qualifiers::OCL_None: llvm_unreachable("none but true"); 01538 case Qualifiers::OCL_ExplicitNone: OS << "__unsafe_unretained"; break; 01539 case Qualifiers::OCL_Strong: 01540 if (!Policy.SuppressStrongLifetime) 01541 OS << "__strong"; 01542 break; 01543 01544 case Qualifiers::OCL_Weak: OS << "__weak"; break; 01545 case Qualifiers::OCL_Autoreleasing: OS << "__autoreleasing"; break; 01546 } 01547 } 01548 01549 if (appendSpaceIfNonEmpty && addSpace) 01550 OS << ' '; 01551 } 01552 01553 std::string QualType::getAsString(const PrintingPolicy &Policy) const { 01554 std::string S; 01555 getAsStringInternal(S, Policy); 01556 return S; 01557 } 01558 01559 std::string QualType::getAsString(const Type *ty, Qualifiers qs) { 01560 std::string buffer; 01561 LangOptions options; 01562 getAsStringInternal(ty, qs, buffer, PrintingPolicy(options)); 01563 return buffer; 01564 } 01565 01566 void QualType::print(const Type *ty, Qualifiers qs, 01567 raw_ostream &OS, const PrintingPolicy &policy, 01568 const Twine &PlaceHolder) { 01569 SmallString<128> PHBuf; 01570 StringRef PH; 01571 if (PlaceHolder.isSingleStringRef()) 01572 PH = PlaceHolder.getSingleStringRef(); 01573 else 01574 PH = PlaceHolder.toStringRef(PHBuf); 01575 01576 TypePrinter(policy).print(ty, qs, OS, PH); 01577 } 01578 01579 void QualType::getAsStringInternal(const Type *ty, Qualifiers qs, 01580 std::string &buffer, 01581 const PrintingPolicy &policy) { 01582 SmallString<256> Buf; 01583 llvm::raw_svector_ostream StrOS(Buf); 01584 TypePrinter(policy).print(ty, qs, StrOS, buffer); 01585 std::string str = StrOS.str(); 01586 buffer.swap(str); 01587 }