1 //===- TypePrinter.cpp - Pretty-Print Clang Types -------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This contains code to print types from Clang's type system. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/AST/PrettyPrinter.h" 14 #include "clang/AST/ASTContext.h" 15 #include "clang/AST/Decl.h" 16 #include "clang/AST/DeclBase.h" 17 #include "clang/AST/DeclCXX.h" 18 #include "clang/AST/DeclObjC.h" 19 #include "clang/AST/DeclTemplate.h" 20 #include "clang/AST/Expr.h" 21 #include "clang/AST/NestedNameSpecifier.h" 22 #include "clang/AST/TemplateBase.h" 23 #include "clang/AST/TemplateName.h" 24 #include "clang/AST/Type.h" 25 #include "clang/Basic/AddressSpaces.h" 26 #include "clang/Basic/ExceptionSpecificationType.h" 27 #include "clang/Basic/IdentifierTable.h" 28 #include "clang/Basic/LLVM.h" 29 #include "clang/Basic/LangOptions.h" 30 #include "clang/Basic/SourceLocation.h" 31 #include "clang/Basic/SourceManager.h" 32 #include "clang/Basic/Specifiers.h" 33 #include "llvm/ADT/ArrayRef.h" 34 #include "llvm/ADT/SmallString.h" 35 #include "llvm/ADT/StringRef.h" 36 #include "llvm/ADT/Twine.h" 37 #include "llvm/Support/Casting.h" 38 #include "llvm/Support/Compiler.h" 39 #include "llvm/Support/ErrorHandling.h" 40 #include "llvm/Support/SaveAndRestore.h" 41 #include "llvm/Support/raw_ostream.h" 42 #include <cassert> 43 #include <string> 44 45 using namespace clang; 46 47 namespace { 48 49 /// RAII object that enables printing of the ARC __strong lifetime 50 /// qualifier. 51 class IncludeStrongLifetimeRAII { 52 PrintingPolicy &Policy; 53 bool Old; 54 55 public: 56 explicit IncludeStrongLifetimeRAII(PrintingPolicy &Policy) 57 : Policy(Policy), Old(Policy.SuppressStrongLifetime) { 58 if (!Policy.SuppressLifetimeQualifiers) 59 Policy.SuppressStrongLifetime = false; 60 } 61 62 ~IncludeStrongLifetimeRAII() { 63 Policy.SuppressStrongLifetime = Old; 64 } 65 }; 66 67 class ParamPolicyRAII { 68 PrintingPolicy &Policy; 69 bool Old; 70 71 public: 72 explicit ParamPolicyRAII(PrintingPolicy &Policy) 73 : Policy(Policy), Old(Policy.SuppressSpecifiers) { 74 Policy.SuppressSpecifiers = false; 75 } 76 77 ~ParamPolicyRAII() { 78 Policy.SuppressSpecifiers = Old; 79 } 80 }; 81 82 class ElaboratedTypePolicyRAII { 83 PrintingPolicy &Policy; 84 bool SuppressTagKeyword; 85 bool SuppressScope; 86 87 public: 88 explicit ElaboratedTypePolicyRAII(PrintingPolicy &Policy) : Policy(Policy) { 89 SuppressTagKeyword = Policy.SuppressTagKeyword; 90 SuppressScope = Policy.SuppressScope; 91 Policy.SuppressTagKeyword = true; 92 Policy.SuppressScope = true; 93 } 94 95 ~ElaboratedTypePolicyRAII() { 96 Policy.SuppressTagKeyword = SuppressTagKeyword; 97 Policy.SuppressScope = SuppressScope; 98 } 99 }; 100 101 class TypePrinter { 102 PrintingPolicy Policy; 103 unsigned Indentation; 104 bool HasEmptyPlaceHolder = false; 105 bool InsideCCAttribute = false; 106 107 public: 108 explicit TypePrinter(const PrintingPolicy &Policy, unsigned Indentation = 0) 109 : Policy(Policy), Indentation(Indentation) {} 110 111 void print(const Type *ty, Qualifiers qs, raw_ostream &OS, 112 StringRef PlaceHolder); 113 void print(QualType T, raw_ostream &OS, StringRef PlaceHolder); 114 115 static bool canPrefixQualifiers(const Type *T, bool &NeedARCStrongQualifier); 116 void spaceBeforePlaceHolder(raw_ostream &OS); 117 void printTypeSpec(NamedDecl *D, raw_ostream &OS); 118 119 void printBefore(QualType T, raw_ostream &OS); 120 void printAfter(QualType T, raw_ostream &OS); 121 void AppendScope(DeclContext *DC, raw_ostream &OS); 122 void printTag(TagDecl *T, raw_ostream &OS); 123 void printFunctionAfter(const FunctionType::ExtInfo &Info, raw_ostream &OS); 124 #define ABSTRACT_TYPE(CLASS, PARENT) 125 #define TYPE(CLASS, PARENT) \ 126 void print##CLASS##Before(const CLASS##Type *T, raw_ostream &OS); \ 127 void print##CLASS##After(const CLASS##Type *T, raw_ostream &OS); 128 #include "clang/AST/TypeNodes.def" 129 130 private: 131 void printBefore(const Type *ty, Qualifiers qs, raw_ostream &OS); 132 void printAfter(const Type *ty, Qualifiers qs, raw_ostream &OS); 133 }; 134 135 } // namespace 136 137 static void AppendTypeQualList(raw_ostream &OS, unsigned TypeQuals, 138 bool HasRestrictKeyword) { 139 bool appendSpace = false; 140 if (TypeQuals & Qualifiers::Const) { 141 OS << "const"; 142 appendSpace = true; 143 } 144 if (TypeQuals & Qualifiers::Volatile) { 145 if (appendSpace) OS << ' '; 146 OS << "volatile"; 147 appendSpace = true; 148 } 149 if (TypeQuals & Qualifiers::Restrict) { 150 if (appendSpace) OS << ' '; 151 if (HasRestrictKeyword) { 152 OS << "restrict"; 153 } else { 154 OS << "__restrict"; 155 } 156 } 157 } 158 159 void TypePrinter::spaceBeforePlaceHolder(raw_ostream &OS) { 160 if (!HasEmptyPlaceHolder) 161 OS << ' '; 162 } 163 164 static SplitQualType splitAccordingToPolicy(QualType QT, 165 const PrintingPolicy &Policy) { 166 if (Policy.PrintCanonicalTypes) 167 QT = QT.getCanonicalType(); 168 return QT.split(); 169 } 170 171 void TypePrinter::print(QualType t, raw_ostream &OS, StringRef PlaceHolder) { 172 SplitQualType split = splitAccordingToPolicy(t, Policy); 173 print(split.Ty, split.Quals, OS, PlaceHolder); 174 } 175 176 void TypePrinter::print(const Type *T, Qualifiers Quals, raw_ostream &OS, 177 StringRef PlaceHolder) { 178 if (!T) { 179 OS << "NULL TYPE"; 180 return; 181 } 182 183 SaveAndRestore<bool> PHVal(HasEmptyPlaceHolder, PlaceHolder.empty()); 184 185 printBefore(T, Quals, OS); 186 OS << PlaceHolder; 187 printAfter(T, Quals, OS); 188 } 189 190 bool TypePrinter::canPrefixQualifiers(const Type *T, 191 bool &NeedARCStrongQualifier) { 192 // CanPrefixQualifiers - We prefer to print type qualifiers before the type, 193 // so that we get "const int" instead of "int const", but we can't do this if 194 // the type is complex. For example if the type is "int*", we *must* print 195 // "int * const", printing "const int *" is different. Only do this when the 196 // type expands to a simple string. 197 bool CanPrefixQualifiers = false; 198 NeedARCStrongQualifier = false; 199 Type::TypeClass TC = T->getTypeClass(); 200 if (const auto *AT = dyn_cast<AutoType>(T)) 201 TC = AT->desugar()->getTypeClass(); 202 if (const auto *Subst = dyn_cast<SubstTemplateTypeParmType>(T)) 203 TC = Subst->getReplacementType()->getTypeClass(); 204 205 switch (TC) { 206 case Type::Auto: 207 case Type::Builtin: 208 case Type::Complex: 209 case Type::UnresolvedUsing: 210 case Type::Typedef: 211 case Type::TypeOfExpr: 212 case Type::TypeOf: 213 case Type::Decltype: 214 case Type::UnaryTransform: 215 case Type::Record: 216 case Type::Enum: 217 case Type::Elaborated: 218 case Type::TemplateTypeParm: 219 case Type::SubstTemplateTypeParmPack: 220 case Type::DeducedTemplateSpecialization: 221 case Type::TemplateSpecialization: 222 case Type::InjectedClassName: 223 case Type::DependentName: 224 case Type::DependentTemplateSpecialization: 225 case Type::ObjCObject: 226 case Type::ObjCTypeParam: 227 case Type::ObjCInterface: 228 case Type::Atomic: 229 case Type::Pipe: 230 CanPrefixQualifiers = true; 231 break; 232 233 case Type::ObjCObjectPointer: 234 CanPrefixQualifiers = T->isObjCIdType() || T->isObjCClassType() || 235 T->isObjCQualifiedIdType() || T->isObjCQualifiedClassType(); 236 break; 237 238 case Type::ConstantArray: 239 case Type::IncompleteArray: 240 case Type::VariableArray: 241 case Type::DependentSizedArray: 242 NeedARCStrongQualifier = true; 243 LLVM_FALLTHROUGH; 244 245 case Type::Adjusted: 246 case Type::Decayed: 247 case Type::Pointer: 248 case Type::BlockPointer: 249 case Type::LValueReference: 250 case Type::RValueReference: 251 case Type::MemberPointer: 252 case Type::DependentAddressSpace: 253 case Type::DependentVector: 254 case Type::DependentSizedExtVector: 255 case Type::Vector: 256 case Type::ExtVector: 257 case Type::FunctionProto: 258 case Type::FunctionNoProto: 259 case Type::Paren: 260 case Type::PackExpansion: 261 case Type::SubstTemplateTypeParm: 262 case Type::MacroQualified: 263 CanPrefixQualifiers = false; 264 break; 265 266 case Type::Attributed: { 267 // We still want to print the address_space before the type if it is an 268 // address_space attribute. 269 const auto *AttrTy = cast<AttributedType>(T); 270 CanPrefixQualifiers = AttrTy->getAttrKind() == attr::AddressSpace; 271 } 272 } 273 274 return CanPrefixQualifiers; 275 } 276 277 void TypePrinter::printBefore(QualType T, raw_ostream &OS) { 278 SplitQualType Split = splitAccordingToPolicy(T, Policy); 279 280 // If we have cv1 T, where T is substituted for cv2 U, only print cv1 - cv2 281 // at this level. 282 Qualifiers Quals = Split.Quals; 283 if (const auto *Subst = dyn_cast<SubstTemplateTypeParmType>(Split.Ty)) 284 Quals -= QualType(Subst, 0).getQualifiers(); 285 286 printBefore(Split.Ty, Quals, OS); 287 } 288 289 /// Prints the part of the type string before an identifier, e.g. for 290 /// "int foo[10]" it prints "int ". 291 void TypePrinter::printBefore(const Type *T,Qualifiers Quals, raw_ostream &OS) { 292 if (Policy.SuppressSpecifiers && T->isSpecifierType()) 293 return; 294 295 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder); 296 297 // Print qualifiers as appropriate. 298 299 bool CanPrefixQualifiers = false; 300 bool NeedARCStrongQualifier = false; 301 CanPrefixQualifiers = canPrefixQualifiers(T, NeedARCStrongQualifier); 302 303 if (CanPrefixQualifiers && !Quals.empty()) { 304 if (NeedARCStrongQualifier) { 305 IncludeStrongLifetimeRAII Strong(Policy); 306 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/true); 307 } else { 308 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/true); 309 } 310 } 311 312 bool hasAfterQuals = false; 313 if (!CanPrefixQualifiers && !Quals.empty()) { 314 hasAfterQuals = !Quals.isEmptyWhenPrinted(Policy); 315 if (hasAfterQuals) 316 HasEmptyPlaceHolder = false; 317 } 318 319 switch (T->getTypeClass()) { 320 #define ABSTRACT_TYPE(CLASS, PARENT) 321 #define TYPE(CLASS, PARENT) case Type::CLASS: \ 322 print##CLASS##Before(cast<CLASS##Type>(T), OS); \ 323 break; 324 #include "clang/AST/TypeNodes.def" 325 } 326 327 if (hasAfterQuals) { 328 if (NeedARCStrongQualifier) { 329 IncludeStrongLifetimeRAII Strong(Policy); 330 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/!PrevPHIsEmpty.get()); 331 } else { 332 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/!PrevPHIsEmpty.get()); 333 } 334 } 335 } 336 337 void TypePrinter::printAfter(QualType t, raw_ostream &OS) { 338 SplitQualType split = splitAccordingToPolicy(t, Policy); 339 printAfter(split.Ty, split.Quals, OS); 340 } 341 342 /// Prints the part of the type string after an identifier, e.g. for 343 /// "int foo[10]" it prints "[10]". 344 void TypePrinter::printAfter(const Type *T, Qualifiers Quals, raw_ostream &OS) { 345 switch (T->getTypeClass()) { 346 #define ABSTRACT_TYPE(CLASS, PARENT) 347 #define TYPE(CLASS, PARENT) case Type::CLASS: \ 348 print##CLASS##After(cast<CLASS##Type>(T), OS); \ 349 break; 350 #include "clang/AST/TypeNodes.def" 351 } 352 } 353 354 void TypePrinter::printBuiltinBefore(const BuiltinType *T, raw_ostream &OS) { 355 OS << T->getName(Policy); 356 spaceBeforePlaceHolder(OS); 357 } 358 359 void TypePrinter::printBuiltinAfter(const BuiltinType *T, raw_ostream &OS) {} 360 361 void TypePrinter::printComplexBefore(const ComplexType *T, raw_ostream &OS) { 362 OS << "_Complex "; 363 printBefore(T->getElementType(), OS); 364 } 365 366 void TypePrinter::printComplexAfter(const ComplexType *T, raw_ostream &OS) { 367 printAfter(T->getElementType(), OS); 368 } 369 370 void TypePrinter::printPointerBefore(const PointerType *T, raw_ostream &OS) { 371 IncludeStrongLifetimeRAII Strong(Policy); 372 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 373 printBefore(T->getPointeeType(), OS); 374 // Handle things like 'int (*A)[4];' correctly. 375 // FIXME: this should include vectors, but vectors use attributes I guess. 376 if (isa<ArrayType>(T->getPointeeType())) 377 OS << '('; 378 OS << '*'; 379 } 380 381 void TypePrinter::printPointerAfter(const PointerType *T, raw_ostream &OS) { 382 IncludeStrongLifetimeRAII Strong(Policy); 383 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 384 // Handle things like 'int (*A)[4];' correctly. 385 // FIXME: this should include vectors, but vectors use attributes I guess. 386 if (isa<ArrayType>(T->getPointeeType())) 387 OS << ')'; 388 printAfter(T->getPointeeType(), OS); 389 } 390 391 void TypePrinter::printBlockPointerBefore(const BlockPointerType *T, 392 raw_ostream &OS) { 393 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 394 printBefore(T->getPointeeType(), OS); 395 OS << '^'; 396 } 397 398 void TypePrinter::printBlockPointerAfter(const BlockPointerType *T, 399 raw_ostream &OS) { 400 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 401 printAfter(T->getPointeeType(), OS); 402 } 403 404 // When printing a reference, the referenced type might also be a reference. 405 // If so, we want to skip that before printing the inner type. 406 static QualType skipTopLevelReferences(QualType T) { 407 if (auto *Ref = T->getAs<ReferenceType>()) 408 return skipTopLevelReferences(Ref->getPointeeTypeAsWritten()); 409 return T; 410 } 411 412 void TypePrinter::printLValueReferenceBefore(const LValueReferenceType *T, 413 raw_ostream &OS) { 414 IncludeStrongLifetimeRAII Strong(Policy); 415 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 416 QualType Inner = skipTopLevelReferences(T->getPointeeTypeAsWritten()); 417 printBefore(Inner, OS); 418 // Handle things like 'int (&A)[4];' correctly. 419 // FIXME: this should include vectors, but vectors use attributes I guess. 420 if (isa<ArrayType>(Inner)) 421 OS << '('; 422 OS << '&'; 423 } 424 425 void TypePrinter::printLValueReferenceAfter(const LValueReferenceType *T, 426 raw_ostream &OS) { 427 IncludeStrongLifetimeRAII Strong(Policy); 428 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 429 QualType Inner = skipTopLevelReferences(T->getPointeeTypeAsWritten()); 430 // Handle things like 'int (&A)[4];' correctly. 431 // FIXME: this should include vectors, but vectors use attributes I guess. 432 if (isa<ArrayType>(Inner)) 433 OS << ')'; 434 printAfter(Inner, OS); 435 } 436 437 void TypePrinter::printRValueReferenceBefore(const RValueReferenceType *T, 438 raw_ostream &OS) { 439 IncludeStrongLifetimeRAII Strong(Policy); 440 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 441 QualType Inner = skipTopLevelReferences(T->getPointeeTypeAsWritten()); 442 printBefore(Inner, OS); 443 // Handle things like 'int (&&A)[4];' correctly. 444 // FIXME: this should include vectors, but vectors use attributes I guess. 445 if (isa<ArrayType>(Inner)) 446 OS << '('; 447 OS << "&&"; 448 } 449 450 void TypePrinter::printRValueReferenceAfter(const RValueReferenceType *T, 451 raw_ostream &OS) { 452 IncludeStrongLifetimeRAII Strong(Policy); 453 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 454 QualType Inner = skipTopLevelReferences(T->getPointeeTypeAsWritten()); 455 // Handle things like 'int (&&A)[4];' correctly. 456 // FIXME: this should include vectors, but vectors use attributes I guess. 457 if (isa<ArrayType>(Inner)) 458 OS << ')'; 459 printAfter(Inner, OS); 460 } 461 462 void TypePrinter::printMemberPointerBefore(const MemberPointerType *T, 463 raw_ostream &OS) { 464 IncludeStrongLifetimeRAII Strong(Policy); 465 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 466 printBefore(T->getPointeeType(), OS); 467 // Handle things like 'int (Cls::*A)[4];' correctly. 468 // FIXME: this should include vectors, but vectors use attributes I guess. 469 if (isa<ArrayType>(T->getPointeeType())) 470 OS << '('; 471 472 PrintingPolicy InnerPolicy(Policy); 473 InnerPolicy.IncludeTagDefinition = false; 474 TypePrinter(InnerPolicy).print(QualType(T->getClass(), 0), OS, StringRef()); 475 476 OS << "::*"; 477 } 478 479 void TypePrinter::printMemberPointerAfter(const MemberPointerType *T, 480 raw_ostream &OS) { 481 IncludeStrongLifetimeRAII Strong(Policy); 482 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 483 // Handle things like 'int (Cls::*A)[4];' correctly. 484 // FIXME: this should include vectors, but vectors use attributes I guess. 485 if (isa<ArrayType>(T->getPointeeType())) 486 OS << ')'; 487 printAfter(T->getPointeeType(), OS); 488 } 489 490 void TypePrinter::printConstantArrayBefore(const ConstantArrayType *T, 491 raw_ostream &OS) { 492 IncludeStrongLifetimeRAII Strong(Policy); 493 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 494 printBefore(T->getElementType(), OS); 495 } 496 497 void TypePrinter::printConstantArrayAfter(const ConstantArrayType *T, 498 raw_ostream &OS) { 499 OS << '['; 500 if (T->getIndexTypeQualifiers().hasQualifiers()) { 501 AppendTypeQualList(OS, T->getIndexTypeCVRQualifiers(), 502 Policy.Restrict); 503 OS << ' '; 504 } 505 506 if (T->getSizeModifier() == ArrayType::Static) 507 OS << "static "; 508 509 OS << T->getSize().getZExtValue() << ']'; 510 printAfter(T->getElementType(), OS); 511 } 512 513 void TypePrinter::printIncompleteArrayBefore(const IncompleteArrayType *T, 514 raw_ostream &OS) { 515 IncludeStrongLifetimeRAII Strong(Policy); 516 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 517 printBefore(T->getElementType(), OS); 518 } 519 520 void TypePrinter::printIncompleteArrayAfter(const IncompleteArrayType *T, 521 raw_ostream &OS) { 522 OS << "[]"; 523 printAfter(T->getElementType(), OS); 524 } 525 526 void TypePrinter::printVariableArrayBefore(const VariableArrayType *T, 527 raw_ostream &OS) { 528 IncludeStrongLifetimeRAII Strong(Policy); 529 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 530 printBefore(T->getElementType(), OS); 531 } 532 533 void TypePrinter::printVariableArrayAfter(const VariableArrayType *T, 534 raw_ostream &OS) { 535 OS << '['; 536 if (T->getIndexTypeQualifiers().hasQualifiers()) { 537 AppendTypeQualList(OS, T->getIndexTypeCVRQualifiers(), Policy.Restrict); 538 OS << ' '; 539 } 540 541 if (T->getSizeModifier() == VariableArrayType::Static) 542 OS << "static "; 543 else if (T->getSizeModifier() == VariableArrayType::Star) 544 OS << '*'; 545 546 if (T->getSizeExpr()) 547 T->getSizeExpr()->printPretty(OS, nullptr, Policy); 548 OS << ']'; 549 550 printAfter(T->getElementType(), OS); 551 } 552 553 void TypePrinter::printAdjustedBefore(const AdjustedType *T, raw_ostream &OS) { 554 // Print the adjusted representation, otherwise the adjustment will be 555 // invisible. 556 printBefore(T->getAdjustedType(), OS); 557 } 558 559 void TypePrinter::printAdjustedAfter(const AdjustedType *T, raw_ostream &OS) { 560 printAfter(T->getAdjustedType(), OS); 561 } 562 563 void TypePrinter::printDecayedBefore(const DecayedType *T, raw_ostream &OS) { 564 // Print as though it's a pointer. 565 printAdjustedBefore(T, OS); 566 } 567 568 void TypePrinter::printDecayedAfter(const DecayedType *T, raw_ostream &OS) { 569 printAdjustedAfter(T, OS); 570 } 571 572 void TypePrinter::printDependentSizedArrayBefore( 573 const DependentSizedArrayType *T, 574 raw_ostream &OS) { 575 IncludeStrongLifetimeRAII Strong(Policy); 576 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 577 printBefore(T->getElementType(), OS); 578 } 579 580 void TypePrinter::printDependentSizedArrayAfter( 581 const DependentSizedArrayType *T, 582 raw_ostream &OS) { 583 OS << '['; 584 if (T->getSizeExpr()) 585 T->getSizeExpr()->printPretty(OS, nullptr, Policy); 586 OS << ']'; 587 printAfter(T->getElementType(), OS); 588 } 589 590 void TypePrinter::printDependentAddressSpaceBefore( 591 const DependentAddressSpaceType *T, raw_ostream &OS) { 592 printBefore(T->getPointeeType(), OS); 593 } 594 595 void TypePrinter::printDependentAddressSpaceAfter( 596 const DependentAddressSpaceType *T, raw_ostream &OS) { 597 OS << " __attribute__((address_space("; 598 if (T->getAddrSpaceExpr()) 599 T->getAddrSpaceExpr()->printPretty(OS, nullptr, Policy); 600 OS << ")))"; 601 printAfter(T->getPointeeType(), OS); 602 } 603 604 void TypePrinter::printDependentSizedExtVectorBefore( 605 const DependentSizedExtVectorType *T, 606 raw_ostream &OS) { 607 printBefore(T->getElementType(), OS); 608 } 609 610 void TypePrinter::printDependentSizedExtVectorAfter( 611 const DependentSizedExtVectorType *T, 612 raw_ostream &OS) { 613 OS << " __attribute__((ext_vector_type("; 614 if (T->getSizeExpr()) 615 T->getSizeExpr()->printPretty(OS, nullptr, Policy); 616 OS << ")))"; 617 printAfter(T->getElementType(), OS); 618 } 619 620 void TypePrinter::printVectorBefore(const VectorType *T, raw_ostream &OS) { 621 switch (T->getVectorKind()) { 622 case VectorType::AltiVecPixel: 623 OS << "__vector __pixel "; 624 break; 625 case VectorType::AltiVecBool: 626 OS << "__vector __bool "; 627 printBefore(T->getElementType(), OS); 628 break; 629 case VectorType::AltiVecVector: 630 OS << "__vector "; 631 printBefore(T->getElementType(), OS); 632 break; 633 case VectorType::NeonVector: 634 OS << "__attribute__((neon_vector_type(" 635 << T->getNumElements() << "))) "; 636 printBefore(T->getElementType(), OS); 637 break; 638 case VectorType::NeonPolyVector: 639 OS << "__attribute__((neon_polyvector_type(" << 640 T->getNumElements() << "))) "; 641 printBefore(T->getElementType(), OS); 642 break; 643 case VectorType::GenericVector: { 644 // FIXME: We prefer to print the size directly here, but have no way 645 // to get the size of the type. 646 OS << "__attribute__((__vector_size__(" 647 << T->getNumElements() 648 << " * sizeof("; 649 print(T->getElementType(), OS, StringRef()); 650 OS << ")))) "; 651 printBefore(T->getElementType(), OS); 652 break; 653 } 654 } 655 } 656 657 void TypePrinter::printVectorAfter(const VectorType *T, raw_ostream &OS) { 658 printAfter(T->getElementType(), OS); 659 } 660 661 void TypePrinter::printDependentVectorBefore( 662 const DependentVectorType *T, raw_ostream &OS) { 663 switch (T->getVectorKind()) { 664 case VectorType::AltiVecPixel: 665 OS << "__vector __pixel "; 666 break; 667 case VectorType::AltiVecBool: 668 OS << "__vector __bool "; 669 printBefore(T->getElementType(), OS); 670 break; 671 case VectorType::AltiVecVector: 672 OS << "__vector "; 673 printBefore(T->getElementType(), OS); 674 break; 675 case VectorType::NeonVector: 676 OS << "__attribute__((neon_vector_type("; 677 if (T->getSizeExpr()) 678 T->getSizeExpr()->printPretty(OS, nullptr, Policy); 679 OS << "))) "; 680 printBefore(T->getElementType(), OS); 681 break; 682 case VectorType::NeonPolyVector: 683 OS << "__attribute__((neon_polyvector_type("; 684 if (T->getSizeExpr()) 685 T->getSizeExpr()->printPretty(OS, nullptr, Policy); 686 OS << "))) "; 687 printBefore(T->getElementType(), OS); 688 break; 689 case VectorType::GenericVector: { 690 // FIXME: We prefer to print the size directly here, but have no way 691 // to get the size of the type. 692 OS << "__attribute__((__vector_size__("; 693 if (T->getSizeExpr()) 694 T->getSizeExpr()->printPretty(OS, nullptr, Policy); 695 OS << " * sizeof("; 696 print(T->getElementType(), OS, StringRef()); 697 OS << ")))) "; 698 printBefore(T->getElementType(), OS); 699 break; 700 } 701 } 702 } 703 704 void TypePrinter::printDependentVectorAfter( 705 const DependentVectorType *T, raw_ostream &OS) { 706 printAfter(T->getElementType(), OS); 707 } 708 709 void TypePrinter::printExtVectorBefore(const ExtVectorType *T, 710 raw_ostream &OS) { 711 printBefore(T->getElementType(), OS); 712 } 713 714 void TypePrinter::printExtVectorAfter(const ExtVectorType *T, raw_ostream &OS) { 715 printAfter(T->getElementType(), OS); 716 OS << " __attribute__((ext_vector_type("; 717 OS << T->getNumElements(); 718 OS << ")))"; 719 } 720 721 void 722 FunctionProtoType::printExceptionSpecification(raw_ostream &OS, 723 const PrintingPolicy &Policy) 724 const { 725 if (hasDynamicExceptionSpec()) { 726 OS << " throw("; 727 if (getExceptionSpecType() == EST_MSAny) 728 OS << "..."; 729 else 730 for (unsigned I = 0, N = getNumExceptions(); I != N; ++I) { 731 if (I) 732 OS << ", "; 733 734 OS << getExceptionType(I).stream(Policy); 735 } 736 OS << ')'; 737 } else if (isNoexceptExceptionSpec(getExceptionSpecType())) { 738 OS << " noexcept"; 739 // FIXME:Is it useful to print out the expression for a non-dependent 740 // noexcept specification? 741 if (isComputedNoexcept(getExceptionSpecType())) { 742 OS << '('; 743 if (getNoexceptExpr()) 744 getNoexceptExpr()->printPretty(OS, nullptr, Policy); 745 OS << ')'; 746 } 747 } 748 } 749 750 void TypePrinter::printFunctionProtoBefore(const FunctionProtoType *T, 751 raw_ostream &OS) { 752 if (T->hasTrailingReturn()) { 753 OS << "auto "; 754 if (!HasEmptyPlaceHolder) 755 OS << '('; 756 } else { 757 // If needed for precedence reasons, wrap the inner part in grouping parens. 758 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false); 759 printBefore(T->getReturnType(), OS); 760 if (!PrevPHIsEmpty.get()) 761 OS << '('; 762 } 763 } 764 765 StringRef clang::getParameterABISpelling(ParameterABI ABI) { 766 switch (ABI) { 767 case ParameterABI::Ordinary: 768 llvm_unreachable("asking for spelling of ordinary parameter ABI"); 769 case ParameterABI::SwiftContext: 770 return "swift_context"; 771 case ParameterABI::SwiftErrorResult: 772 return "swift_error_result"; 773 case ParameterABI::SwiftIndirectResult: 774 return "swift_indirect_result"; 775 } 776 llvm_unreachable("bad parameter ABI kind"); 777 } 778 779 void TypePrinter::printFunctionProtoAfter(const FunctionProtoType *T, 780 raw_ostream &OS) { 781 // If needed for precedence reasons, wrap the inner part in grouping parens. 782 if (!HasEmptyPlaceHolder) 783 OS << ')'; 784 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 785 786 OS << '('; 787 { 788 ParamPolicyRAII ParamPolicy(Policy); 789 for (unsigned i = 0, e = T->getNumParams(); i != e; ++i) { 790 if (i) OS << ", "; 791 792 auto EPI = T->getExtParameterInfo(i); 793 if (EPI.isConsumed()) OS << "__attribute__((ns_consumed)) "; 794 if (EPI.isNoEscape()) 795 OS << "__attribute__((noescape)) "; 796 auto ABI = EPI.getABI(); 797 if (ABI != ParameterABI::Ordinary) 798 OS << "__attribute__((" << getParameterABISpelling(ABI) << ")) "; 799 800 print(T->getParamType(i), OS, StringRef()); 801 } 802 } 803 804 if (T->isVariadic()) { 805 if (T->getNumParams()) 806 OS << ", "; 807 OS << "..."; 808 } else if (T->getNumParams() == 0 && Policy.UseVoidForZeroParams) { 809 // Do not emit int() if we have a proto, emit 'int(void)'. 810 OS << "void"; 811 } 812 813 OS << ')'; 814 815 FunctionType::ExtInfo Info = T->getExtInfo(); 816 817 printFunctionAfter(Info, OS); 818 819 if (!T->getMethodQuals().empty()) 820 OS << " " << T->getMethodQuals().getAsString(); 821 822 switch (T->getRefQualifier()) { 823 case RQ_None: 824 break; 825 826 case RQ_LValue: 827 OS << " &"; 828 break; 829 830 case RQ_RValue: 831 OS << " &&"; 832 break; 833 } 834 T->printExceptionSpecification(OS, Policy); 835 836 if (T->hasTrailingReturn()) { 837 OS << " -> "; 838 print(T->getReturnType(), OS, StringRef()); 839 } else 840 printAfter(T->getReturnType(), OS); 841 } 842 843 void TypePrinter::printFunctionAfter(const FunctionType::ExtInfo &Info, 844 raw_ostream &OS) { 845 if (!InsideCCAttribute) { 846 switch (Info.getCC()) { 847 case CC_C: 848 // The C calling convention is the default on the vast majority of platforms 849 // we support. If the user wrote it explicitly, it will usually be printed 850 // while traversing the AttributedType. If the type has been desugared, let 851 // the canonical spelling be the implicit calling convention. 852 // FIXME: It would be better to be explicit in certain contexts, such as a 853 // cdecl function typedef used to declare a member function with the 854 // Microsoft C++ ABI. 855 break; 856 case CC_X86StdCall: 857 OS << " __attribute__((stdcall))"; 858 break; 859 case CC_X86FastCall: 860 OS << " __attribute__((fastcall))"; 861 break; 862 case CC_X86ThisCall: 863 OS << " __attribute__((thiscall))"; 864 break; 865 case CC_X86VectorCall: 866 OS << " __attribute__((vectorcall))"; 867 break; 868 case CC_X86Pascal: 869 OS << " __attribute__((pascal))"; 870 break; 871 case CC_AAPCS: 872 OS << " __attribute__((pcs(\"aapcs\")))"; 873 break; 874 case CC_AAPCS_VFP: 875 OS << " __attribute__((pcs(\"aapcs-vfp\")))"; 876 break; 877 case CC_AArch64VectorCall: 878 OS << "__attribute__((aarch64_vector_pcs))"; 879 break; 880 case CC_IntelOclBicc: 881 OS << " __attribute__((intel_ocl_bicc))"; 882 break; 883 case CC_Win64: 884 OS << " __attribute__((ms_abi))"; 885 break; 886 case CC_X86_64SysV: 887 OS << " __attribute__((sysv_abi))"; 888 break; 889 case CC_X86RegCall: 890 OS << " __attribute__((regcall))"; 891 break; 892 case CC_SpirFunction: 893 case CC_OpenCLKernel: 894 // Do nothing. These CCs are not available as attributes. 895 break; 896 case CC_Swift: 897 OS << " __attribute__((swiftcall))"; 898 break; 899 case CC_PreserveMost: 900 OS << " __attribute__((preserve_most))"; 901 break; 902 case CC_PreserveAll: 903 OS << " __attribute__((preserve_all))"; 904 break; 905 } 906 } 907 908 if (Info.getNoReturn()) 909 OS << " __attribute__((noreturn))"; 910 if (Info.getProducesResult()) 911 OS << " __attribute__((ns_returns_retained))"; 912 if (Info.getRegParm()) 913 OS << " __attribute__((regparm (" 914 << Info.getRegParm() << ")))"; 915 if (Info.getNoCallerSavedRegs()) 916 OS << " __attribute__((no_caller_saved_registers))"; 917 if (Info.getNoCfCheck()) 918 OS << " __attribute__((nocf_check))"; 919 } 920 921 void TypePrinter::printFunctionNoProtoBefore(const FunctionNoProtoType *T, 922 raw_ostream &OS) { 923 // If needed for precedence reasons, wrap the inner part in grouping parens. 924 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false); 925 printBefore(T->getReturnType(), OS); 926 if (!PrevPHIsEmpty.get()) 927 OS << '('; 928 } 929 930 void TypePrinter::printFunctionNoProtoAfter(const FunctionNoProtoType *T, 931 raw_ostream &OS) { 932 // If needed for precedence reasons, wrap the inner part in grouping parens. 933 if (!HasEmptyPlaceHolder) 934 OS << ')'; 935 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 936 937 OS << "()"; 938 printFunctionAfter(T->getExtInfo(), OS); 939 printAfter(T->getReturnType(), OS); 940 } 941 942 void TypePrinter::printTypeSpec(NamedDecl *D, raw_ostream &OS) { 943 944 // Compute the full nested-name-specifier for this type. 945 // In C, this will always be empty except when the type 946 // being printed is anonymous within other Record. 947 if (!Policy.SuppressScope) 948 AppendScope(D->getDeclContext(), OS); 949 950 IdentifierInfo *II = D->getIdentifier(); 951 OS << II->getName(); 952 spaceBeforePlaceHolder(OS); 953 } 954 955 void TypePrinter::printUnresolvedUsingBefore(const UnresolvedUsingType *T, 956 raw_ostream &OS) { 957 printTypeSpec(T->getDecl(), OS); 958 } 959 960 void TypePrinter::printUnresolvedUsingAfter(const UnresolvedUsingType *T, 961 raw_ostream &OS) {} 962 963 void TypePrinter::printTypedefBefore(const TypedefType *T, raw_ostream &OS) { 964 printTypeSpec(T->getDecl(), OS); 965 } 966 967 void TypePrinter::printMacroQualifiedBefore(const MacroQualifiedType *T, 968 raw_ostream &OS) { 969 StringRef MacroName = T->getMacroIdentifier()->getName(); 970 OS << MacroName << " "; 971 972 // Since this type is meant to print the macro instead of the whole attribute, 973 // we trim any attributes and go directly to the original modified type. 974 printBefore(T->getModifiedType(), OS); 975 } 976 977 void TypePrinter::printMacroQualifiedAfter(const MacroQualifiedType *T, 978 raw_ostream &OS) { 979 printAfter(T->getModifiedType(), OS); 980 } 981 982 void TypePrinter::printTypedefAfter(const TypedefType *T, raw_ostream &OS) {} 983 984 void TypePrinter::printTypeOfExprBefore(const TypeOfExprType *T, 985 raw_ostream &OS) { 986 OS << "typeof "; 987 if (T->getUnderlyingExpr()) 988 T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy); 989 spaceBeforePlaceHolder(OS); 990 } 991 992 void TypePrinter::printTypeOfExprAfter(const TypeOfExprType *T, 993 raw_ostream &OS) {} 994 995 void TypePrinter::printTypeOfBefore(const TypeOfType *T, raw_ostream &OS) { 996 OS << "typeof("; 997 print(T->getUnderlyingType(), OS, StringRef()); 998 OS << ')'; 999 spaceBeforePlaceHolder(OS); 1000 } 1001 1002 void TypePrinter::printTypeOfAfter(const TypeOfType *T, raw_ostream &OS) {} 1003 1004 void TypePrinter::printDecltypeBefore(const DecltypeType *T, raw_ostream &OS) { 1005 OS << "decltype("; 1006 if (T->getUnderlyingExpr()) 1007 T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy); 1008 OS << ')'; 1009 spaceBeforePlaceHolder(OS); 1010 } 1011 1012 void TypePrinter::printDecltypeAfter(const DecltypeType *T, raw_ostream &OS) {} 1013 1014 void TypePrinter::printUnaryTransformBefore(const UnaryTransformType *T, 1015 raw_ostream &OS) { 1016 IncludeStrongLifetimeRAII Strong(Policy); 1017 1018 switch (T->getUTTKind()) { 1019 case UnaryTransformType::EnumUnderlyingType: 1020 OS << "__underlying_type("; 1021 print(T->getBaseType(), OS, StringRef()); 1022 OS << ')'; 1023 spaceBeforePlaceHolder(OS); 1024 return; 1025 } 1026 1027 printBefore(T->getBaseType(), OS); 1028 } 1029 1030 void TypePrinter::printUnaryTransformAfter(const UnaryTransformType *T, 1031 raw_ostream &OS) { 1032 IncludeStrongLifetimeRAII Strong(Policy); 1033 1034 switch (T->getUTTKind()) { 1035 case UnaryTransformType::EnumUnderlyingType: 1036 return; 1037 } 1038 1039 printAfter(T->getBaseType(), OS); 1040 } 1041 1042 void TypePrinter::printAutoBefore(const AutoType *T, raw_ostream &OS) { 1043 // If the type has been deduced, do not print 'auto'. 1044 if (!T->getDeducedType().isNull()) { 1045 printBefore(T->getDeducedType(), OS); 1046 } else { 1047 switch (T->getKeyword()) { 1048 case AutoTypeKeyword::Auto: OS << "auto"; break; 1049 case AutoTypeKeyword::DecltypeAuto: OS << "decltype(auto)"; break; 1050 case AutoTypeKeyword::GNUAutoType: OS << "__auto_type"; break; 1051 } 1052 spaceBeforePlaceHolder(OS); 1053 } 1054 } 1055 1056 void TypePrinter::printAutoAfter(const AutoType *T, raw_ostream &OS) { 1057 // If the type has been deduced, do not print 'auto'. 1058 if (!T->getDeducedType().isNull()) 1059 printAfter(T->getDeducedType(), OS); 1060 } 1061 1062 void TypePrinter::printDeducedTemplateSpecializationBefore( 1063 const DeducedTemplateSpecializationType *T, raw_ostream &OS) { 1064 // If the type has been deduced, print the deduced type. 1065 if (!T->getDeducedType().isNull()) { 1066 printBefore(T->getDeducedType(), OS); 1067 } else { 1068 IncludeStrongLifetimeRAII Strong(Policy); 1069 T->getTemplateName().print(OS, Policy); 1070 spaceBeforePlaceHolder(OS); 1071 } 1072 } 1073 1074 void TypePrinter::printDeducedTemplateSpecializationAfter( 1075 const DeducedTemplateSpecializationType *T, raw_ostream &OS) { 1076 // If the type has been deduced, print the deduced type. 1077 if (!T->getDeducedType().isNull()) 1078 printAfter(T->getDeducedType(), OS); 1079 } 1080 1081 void TypePrinter::printAtomicBefore(const AtomicType *T, raw_ostream &OS) { 1082 IncludeStrongLifetimeRAII Strong(Policy); 1083 1084 OS << "_Atomic("; 1085 print(T->getValueType(), OS, StringRef()); 1086 OS << ')'; 1087 spaceBeforePlaceHolder(OS); 1088 } 1089 1090 void TypePrinter::printAtomicAfter(const AtomicType *T, raw_ostream &OS) {} 1091 1092 void TypePrinter::printPipeBefore(const PipeType *T, raw_ostream &OS) { 1093 IncludeStrongLifetimeRAII Strong(Policy); 1094 1095 if (T->isReadOnly()) 1096 OS << "read_only "; 1097 else 1098 OS << "write_only "; 1099 OS << "pipe "; 1100 print(T->getElementType(), OS, StringRef()); 1101 spaceBeforePlaceHolder(OS); 1102 } 1103 1104 void TypePrinter::printPipeAfter(const PipeType *T, raw_ostream &OS) {} 1105 1106 /// Appends the given scope to the end of a string. 1107 void TypePrinter::AppendScope(DeclContext *DC, raw_ostream &OS) { 1108 if (DC->isTranslationUnit()) return; 1109 if (DC->isFunctionOrMethod()) return; 1110 AppendScope(DC->getParent(), OS); 1111 1112 if (const auto *NS = dyn_cast<NamespaceDecl>(DC)) { 1113 if (Policy.SuppressUnwrittenScope && 1114 (NS->isAnonymousNamespace() || NS->isInline())) 1115 return; 1116 if (NS->getIdentifier()) 1117 OS << NS->getName() << "::"; 1118 else 1119 OS << "(anonymous namespace)::"; 1120 } else if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(DC)) { 1121 IncludeStrongLifetimeRAII Strong(Policy); 1122 OS << Spec->getIdentifier()->getName(); 1123 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 1124 printTemplateArgumentList(OS, TemplateArgs.asArray(), Policy); 1125 OS << "::"; 1126 } else if (const auto *Tag = dyn_cast<TagDecl>(DC)) { 1127 if (TypedefNameDecl *Typedef = Tag->getTypedefNameForAnonDecl()) 1128 OS << Typedef->getIdentifier()->getName() << "::"; 1129 else if (Tag->getIdentifier()) 1130 OS << Tag->getIdentifier()->getName() << "::"; 1131 else 1132 return; 1133 } 1134 } 1135 1136 void TypePrinter::printTag(TagDecl *D, raw_ostream &OS) { 1137 if (Policy.IncludeTagDefinition) { 1138 PrintingPolicy SubPolicy = Policy; 1139 SubPolicy.IncludeTagDefinition = false; 1140 D->print(OS, SubPolicy, Indentation); 1141 spaceBeforePlaceHolder(OS); 1142 return; 1143 } 1144 1145 bool HasKindDecoration = false; 1146 1147 // We don't print tags unless this is an elaborated type. 1148 // In C, we just assume every RecordType is an elaborated type. 1149 if (!Policy.SuppressTagKeyword && !D->getTypedefNameForAnonDecl()) { 1150 HasKindDecoration = true; 1151 OS << D->getKindName(); 1152 OS << ' '; 1153 } 1154 1155 // Compute the full nested-name-specifier for this type. 1156 // In C, this will always be empty except when the type 1157 // being printed is anonymous within other Record. 1158 if (!Policy.SuppressScope) 1159 AppendScope(D->getDeclContext(), OS); 1160 1161 if (const IdentifierInfo *II = D->getIdentifier()) 1162 OS << II->getName(); 1163 else if (TypedefNameDecl *Typedef = D->getTypedefNameForAnonDecl()) { 1164 assert(Typedef->getIdentifier() && "Typedef without identifier?"); 1165 OS << Typedef->getIdentifier()->getName(); 1166 } else { 1167 // Make an unambiguous representation for anonymous types, e.g. 1168 // (anonymous enum at /usr/include/string.h:120:9) 1169 OS << (Policy.MSVCFormatting ? '`' : '('); 1170 1171 if (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda()) { 1172 OS << "lambda"; 1173 HasKindDecoration = true; 1174 } else { 1175 OS << "anonymous"; 1176 } 1177 1178 if (Policy.AnonymousTagLocations) { 1179 // Suppress the redundant tag keyword if we just printed one. 1180 // We don't have to worry about ElaboratedTypes here because you can't 1181 // refer to an anonymous type with one. 1182 if (!HasKindDecoration) 1183 OS << " " << D->getKindName(); 1184 1185 PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc( 1186 D->getLocation()); 1187 if (PLoc.isValid()) { 1188 OS << " at "; 1189 StringRef File = PLoc.getFilename(); 1190 if (Policy.RemapFilePaths) 1191 OS << Policy.remapPath(File); 1192 else 1193 OS << File; 1194 OS << ':' << PLoc.getLine() << ':' << PLoc.getColumn(); 1195 } 1196 } 1197 1198 OS << (Policy.MSVCFormatting ? '\'' : ')'); 1199 } 1200 1201 // If this is a class template specialization, print the template 1202 // arguments. 1203 if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(D)) { 1204 ArrayRef<TemplateArgument> Args; 1205 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) { 1206 const TemplateSpecializationType *TST = 1207 cast<TemplateSpecializationType>(TAW->getType()); 1208 Args = TST->template_arguments(); 1209 } else { 1210 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 1211 Args = TemplateArgs.asArray(); 1212 } 1213 IncludeStrongLifetimeRAII Strong(Policy); 1214 printTemplateArgumentList(OS, Args, Policy); 1215 } 1216 1217 spaceBeforePlaceHolder(OS); 1218 } 1219 1220 void TypePrinter::printRecordBefore(const RecordType *T, raw_ostream &OS) { 1221 printTag(T->getDecl(), OS); 1222 } 1223 1224 void TypePrinter::printRecordAfter(const RecordType *T, raw_ostream &OS) {} 1225 1226 void TypePrinter::printEnumBefore(const EnumType *T, raw_ostream &OS) { 1227 printTag(T->getDecl(), OS); 1228 } 1229 1230 void TypePrinter::printEnumAfter(const EnumType *T, raw_ostream &OS) {} 1231 1232 void TypePrinter::printTemplateTypeParmBefore(const TemplateTypeParmType *T, 1233 raw_ostream &OS) { 1234 if (IdentifierInfo *Id = T->getIdentifier()) 1235 OS << Id->getName(); 1236 else { 1237 bool IsLambdaAutoParam = false; 1238 if (auto D = T->getDecl()) { 1239 if (auto M = dyn_cast_or_null<CXXMethodDecl>(D->getDeclContext())) 1240 IsLambdaAutoParam = D->isImplicit() && M->getParent()->isLambda(); 1241 } 1242 1243 if (IsLambdaAutoParam) 1244 OS << "auto"; 1245 else 1246 OS << "type-parameter-" << T->getDepth() << '-' << T->getIndex(); 1247 } 1248 spaceBeforePlaceHolder(OS); 1249 } 1250 1251 void TypePrinter::printTemplateTypeParmAfter(const TemplateTypeParmType *T, 1252 raw_ostream &OS) {} 1253 1254 void TypePrinter::printSubstTemplateTypeParmBefore( 1255 const SubstTemplateTypeParmType *T, 1256 raw_ostream &OS) { 1257 IncludeStrongLifetimeRAII Strong(Policy); 1258 printBefore(T->getReplacementType(), OS); 1259 } 1260 1261 void TypePrinter::printSubstTemplateTypeParmAfter( 1262 const SubstTemplateTypeParmType *T, 1263 raw_ostream &OS) { 1264 IncludeStrongLifetimeRAII Strong(Policy); 1265 printAfter(T->getReplacementType(), OS); 1266 } 1267 1268 void TypePrinter::printSubstTemplateTypeParmPackBefore( 1269 const SubstTemplateTypeParmPackType *T, 1270 raw_ostream &OS) { 1271 IncludeStrongLifetimeRAII Strong(Policy); 1272 printTemplateTypeParmBefore(T->getReplacedParameter(), OS); 1273 } 1274 1275 void TypePrinter::printSubstTemplateTypeParmPackAfter( 1276 const SubstTemplateTypeParmPackType *T, 1277 raw_ostream &OS) { 1278 IncludeStrongLifetimeRAII Strong(Policy); 1279 printTemplateTypeParmAfter(T->getReplacedParameter(), OS); 1280 } 1281 1282 void TypePrinter::printTemplateSpecializationBefore( 1283 const TemplateSpecializationType *T, 1284 raw_ostream &OS) { 1285 IncludeStrongLifetimeRAII Strong(Policy); 1286 T->getTemplateName().print(OS, Policy); 1287 1288 printTemplateArgumentList(OS, T->template_arguments(), Policy); 1289 spaceBeforePlaceHolder(OS); 1290 } 1291 1292 void TypePrinter::printTemplateSpecializationAfter( 1293 const TemplateSpecializationType *T, 1294 raw_ostream &OS) {} 1295 1296 void TypePrinter::printInjectedClassNameBefore(const InjectedClassNameType *T, 1297 raw_ostream &OS) { 1298 printTemplateSpecializationBefore(T->getInjectedTST(), OS); 1299 } 1300 1301 void TypePrinter::printInjectedClassNameAfter(const InjectedClassNameType *T, 1302 raw_ostream &OS) {} 1303 1304 void TypePrinter::printElaboratedBefore(const ElaboratedType *T, 1305 raw_ostream &OS) { 1306 if (Policy.IncludeTagDefinition && T->getOwnedTagDecl()) { 1307 TagDecl *OwnedTagDecl = T->getOwnedTagDecl(); 1308 assert(OwnedTagDecl->getTypeForDecl() == T->getNamedType().getTypePtr() && 1309 "OwnedTagDecl expected to be a declaration for the type"); 1310 PrintingPolicy SubPolicy = Policy; 1311 SubPolicy.IncludeTagDefinition = false; 1312 OwnedTagDecl->print(OS, SubPolicy, Indentation); 1313 spaceBeforePlaceHolder(OS); 1314 return; 1315 } 1316 1317 // The tag definition will take care of these. 1318 if (!Policy.IncludeTagDefinition) 1319 { 1320 OS << TypeWithKeyword::getKeywordName(T->getKeyword()); 1321 if (T->getKeyword() != ETK_None) 1322 OS << " "; 1323 NestedNameSpecifier *Qualifier = T->getQualifier(); 1324 if (Qualifier) 1325 Qualifier->print(OS, Policy); 1326 } 1327 1328 ElaboratedTypePolicyRAII PolicyRAII(Policy); 1329 printBefore(T->getNamedType(), OS); 1330 } 1331 1332 void TypePrinter::printElaboratedAfter(const ElaboratedType *T, 1333 raw_ostream &OS) { 1334 if (Policy.IncludeTagDefinition && T->getOwnedTagDecl()) 1335 return; 1336 ElaboratedTypePolicyRAII PolicyRAII(Policy); 1337 printAfter(T->getNamedType(), OS); 1338 } 1339 1340 void TypePrinter::printParenBefore(const ParenType *T, raw_ostream &OS) { 1341 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) { 1342 printBefore(T->getInnerType(), OS); 1343 OS << '('; 1344 } else 1345 printBefore(T->getInnerType(), OS); 1346 } 1347 1348 void TypePrinter::printParenAfter(const ParenType *T, raw_ostream &OS) { 1349 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) { 1350 OS << ')'; 1351 printAfter(T->getInnerType(), OS); 1352 } else 1353 printAfter(T->getInnerType(), OS); 1354 } 1355 1356 void TypePrinter::printDependentNameBefore(const DependentNameType *T, 1357 raw_ostream &OS) { 1358 OS << TypeWithKeyword::getKeywordName(T->getKeyword()); 1359 if (T->getKeyword() != ETK_None) 1360 OS << " "; 1361 1362 T->getQualifier()->print(OS, Policy); 1363 1364 OS << T->getIdentifier()->getName(); 1365 spaceBeforePlaceHolder(OS); 1366 } 1367 1368 void TypePrinter::printDependentNameAfter(const DependentNameType *T, 1369 raw_ostream &OS) {} 1370 1371 void TypePrinter::printDependentTemplateSpecializationBefore( 1372 const DependentTemplateSpecializationType *T, raw_ostream &OS) { 1373 IncludeStrongLifetimeRAII Strong(Policy); 1374 1375 OS << TypeWithKeyword::getKeywordName(T->getKeyword()); 1376 if (T->getKeyword() != ETK_None) 1377 OS << " "; 1378 1379 if (T->getQualifier()) 1380 T->getQualifier()->print(OS, Policy); 1381 OS << T->getIdentifier()->getName(); 1382 printTemplateArgumentList(OS, T->template_arguments(), Policy); 1383 spaceBeforePlaceHolder(OS); 1384 } 1385 1386 void TypePrinter::printDependentTemplateSpecializationAfter( 1387 const DependentTemplateSpecializationType *T, raw_ostream &OS) {} 1388 1389 void TypePrinter::printPackExpansionBefore(const PackExpansionType *T, 1390 raw_ostream &OS) { 1391 printBefore(T->getPattern(), OS); 1392 } 1393 1394 void TypePrinter::printPackExpansionAfter(const PackExpansionType *T, 1395 raw_ostream &OS) { 1396 printAfter(T->getPattern(), OS); 1397 OS << "..."; 1398 } 1399 1400 void TypePrinter::printAttributedBefore(const AttributedType *T, 1401 raw_ostream &OS) { 1402 // FIXME: Generate this with TableGen. 1403 1404 // Prefer the macro forms of the GC and ownership qualifiers. 1405 if (T->getAttrKind() == attr::ObjCGC || 1406 T->getAttrKind() == attr::ObjCOwnership) 1407 return printBefore(T->getEquivalentType(), OS); 1408 1409 if (T->getAttrKind() == attr::ObjCKindOf) 1410 OS << "__kindof "; 1411 1412 if (T->getAttrKind() == attr::AddressSpace) 1413 printBefore(T->getEquivalentType(), OS); 1414 else 1415 printBefore(T->getModifiedType(), OS); 1416 1417 if (T->isMSTypeSpec()) { 1418 switch (T->getAttrKind()) { 1419 default: return; 1420 case attr::Ptr32: OS << " __ptr32"; break; 1421 case attr::Ptr64: OS << " __ptr64"; break; 1422 case attr::SPtr: OS << " __sptr"; break; 1423 case attr::UPtr: OS << " __uptr"; break; 1424 } 1425 spaceBeforePlaceHolder(OS); 1426 } 1427 1428 // Print nullability type specifiers. 1429 if (T->getImmediateNullability()) { 1430 if (T->getAttrKind() == attr::TypeNonNull) 1431 OS << " _Nonnull"; 1432 else if (T->getAttrKind() == attr::TypeNullable) 1433 OS << " _Nullable"; 1434 else if (T->getAttrKind() == attr::TypeNullUnspecified) 1435 OS << " _Null_unspecified"; 1436 else 1437 llvm_unreachable("unhandled nullability"); 1438 spaceBeforePlaceHolder(OS); 1439 } 1440 } 1441 1442 void TypePrinter::printAttributedAfter(const AttributedType *T, 1443 raw_ostream &OS) { 1444 // FIXME: Generate this with TableGen. 1445 1446 // Prefer the macro forms of the GC and ownership qualifiers. 1447 if (T->getAttrKind() == attr::ObjCGC || 1448 T->getAttrKind() == attr::ObjCOwnership) 1449 return printAfter(T->getEquivalentType(), OS); 1450 1451 // If this is a calling convention attribute, don't print the implicit CC from 1452 // the modified type. 1453 SaveAndRestore<bool> MaybeSuppressCC(InsideCCAttribute, T->isCallingConv()); 1454 1455 printAfter(T->getModifiedType(), OS); 1456 1457 // Some attributes are printed as qualifiers before the type, so we have 1458 // nothing left to do. 1459 if (T->getAttrKind() == attr::ObjCKindOf || 1460 T->isMSTypeSpec() || T->getImmediateNullability()) 1461 return; 1462 1463 // Don't print the inert __unsafe_unretained attribute at all. 1464 if (T->getAttrKind() == attr::ObjCInertUnsafeUnretained) 1465 return; 1466 1467 // Don't print ns_returns_retained unless it had an effect. 1468 if (T->getAttrKind() == attr::NSReturnsRetained && 1469 !T->getEquivalentType()->castAs<FunctionType>() 1470 ->getExtInfo().getProducesResult()) 1471 return; 1472 1473 if (T->getAttrKind() == attr::LifetimeBound) { 1474 OS << " [[clang::lifetimebound]]"; 1475 return; 1476 } 1477 1478 // The printing of the address_space attribute is handled by the qualifier 1479 // since it is still stored in the qualifier. Return early to prevent printing 1480 // this twice. 1481 if (T->getAttrKind() == attr::AddressSpace) 1482 return; 1483 1484 OS << " __attribute__(("; 1485 switch (T->getAttrKind()) { 1486 #define TYPE_ATTR(NAME) 1487 #define DECL_OR_TYPE_ATTR(NAME) 1488 #define ATTR(NAME) case attr::NAME: 1489 #include "clang/Basic/AttrList.inc" 1490 llvm_unreachable("non-type attribute attached to type"); 1491 1492 case attr::OpenCLPrivateAddressSpace: 1493 case attr::OpenCLGlobalAddressSpace: 1494 case attr::OpenCLLocalAddressSpace: 1495 case attr::OpenCLConstantAddressSpace: 1496 case attr::OpenCLGenericAddressSpace: 1497 // FIXME: Update printAttributedBefore to print these once we generate 1498 // AttributedType nodes for them. 1499 break; 1500 1501 case attr::LifetimeBound: 1502 case attr::TypeNonNull: 1503 case attr::TypeNullable: 1504 case attr::TypeNullUnspecified: 1505 case attr::ObjCGC: 1506 case attr::ObjCInertUnsafeUnretained: 1507 case attr::ObjCKindOf: 1508 case attr::ObjCOwnership: 1509 case attr::Ptr32: 1510 case attr::Ptr64: 1511 case attr::SPtr: 1512 case attr::UPtr: 1513 case attr::AddressSpace: 1514 llvm_unreachable("This attribute should have been handled already"); 1515 1516 case attr::NSReturnsRetained: 1517 OS << "ns_returns_retained"; 1518 break; 1519 1520 // FIXME: When Sema learns to form this AttributedType, avoid printing the 1521 // attribute again in printFunctionProtoAfter. 1522 case attr::AnyX86NoCfCheck: OS << "nocf_check"; break; 1523 case attr::CDecl: OS << "cdecl"; break; 1524 case attr::FastCall: OS << "fastcall"; break; 1525 case attr::StdCall: OS << "stdcall"; break; 1526 case attr::ThisCall: OS << "thiscall"; break; 1527 case attr::SwiftCall: OS << "swiftcall"; break; 1528 case attr::VectorCall: OS << "vectorcall"; break; 1529 case attr::Pascal: OS << "pascal"; break; 1530 case attr::MSABI: OS << "ms_abi"; break; 1531 case attr::SysVABI: OS << "sysv_abi"; break; 1532 case attr::RegCall: OS << "regcall"; break; 1533 case attr::Pcs: { 1534 OS << "pcs("; 1535 QualType t = T->getEquivalentType(); 1536 while (!t->isFunctionType()) 1537 t = t->getPointeeType(); 1538 OS << (t->getAs<FunctionType>()->getCallConv() == CC_AAPCS ? 1539 "\"aapcs\"" : "\"aapcs-vfp\""); 1540 OS << ')'; 1541 break; 1542 } 1543 case attr::AArch64VectorPcs: OS << "aarch64_vector_pcs"; break; 1544 case attr::IntelOclBicc: OS << "inteloclbicc"; break; 1545 case attr::PreserveMost: 1546 OS << "preserve_most"; 1547 break; 1548 1549 case attr::PreserveAll: 1550 OS << "preserve_all"; 1551 break; 1552 case attr::NoDeref: 1553 OS << "noderef"; 1554 break; 1555 } 1556 OS << "))"; 1557 } 1558 1559 void TypePrinter::printObjCInterfaceBefore(const ObjCInterfaceType *T, 1560 raw_ostream &OS) { 1561 OS << T->getDecl()->getName(); 1562 spaceBeforePlaceHolder(OS); 1563 } 1564 1565 void TypePrinter::printObjCInterfaceAfter(const ObjCInterfaceType *T, 1566 raw_ostream &OS) {} 1567 1568 void TypePrinter::printObjCTypeParamBefore(const ObjCTypeParamType *T, 1569 raw_ostream &OS) { 1570 OS << T->getDecl()->getName(); 1571 if (!T->qual_empty()) { 1572 bool isFirst = true; 1573 OS << '<'; 1574 for (const auto *I : T->quals()) { 1575 if (isFirst) 1576 isFirst = false; 1577 else 1578 OS << ','; 1579 OS << I->getName(); 1580 } 1581 OS << '>'; 1582 } 1583 1584 spaceBeforePlaceHolder(OS); 1585 } 1586 1587 void TypePrinter::printObjCTypeParamAfter(const ObjCTypeParamType *T, 1588 raw_ostream &OS) {} 1589 1590 void TypePrinter::printObjCObjectBefore(const ObjCObjectType *T, 1591 raw_ostream &OS) { 1592 if (T->qual_empty() && T->isUnspecializedAsWritten() && 1593 !T->isKindOfTypeAsWritten()) 1594 return printBefore(T->getBaseType(), OS); 1595 1596 if (T->isKindOfTypeAsWritten()) 1597 OS << "__kindof "; 1598 1599 print(T->getBaseType(), OS, StringRef()); 1600 1601 if (T->isSpecializedAsWritten()) { 1602 bool isFirst = true; 1603 OS << '<'; 1604 for (auto typeArg : T->getTypeArgsAsWritten()) { 1605 if (isFirst) 1606 isFirst = false; 1607 else 1608 OS << ","; 1609 1610 print(typeArg, OS, StringRef()); 1611 } 1612 OS << '>'; 1613 } 1614 1615 if (!T->qual_empty()) { 1616 bool isFirst = true; 1617 OS << '<'; 1618 for (const auto *I : T->quals()) { 1619 if (isFirst) 1620 isFirst = false; 1621 else 1622 OS << ','; 1623 OS << I->getName(); 1624 } 1625 OS << '>'; 1626 } 1627 1628 spaceBeforePlaceHolder(OS); 1629 } 1630 1631 void TypePrinter::printObjCObjectAfter(const ObjCObjectType *T, 1632 raw_ostream &OS) { 1633 if (T->qual_empty() && T->isUnspecializedAsWritten() && 1634 !T->isKindOfTypeAsWritten()) 1635 return printAfter(T->getBaseType(), OS); 1636 } 1637 1638 void TypePrinter::printObjCObjectPointerBefore(const ObjCObjectPointerType *T, 1639 raw_ostream &OS) { 1640 printBefore(T->getPointeeType(), OS); 1641 1642 // If we need to print the pointer, print it now. 1643 if (!T->isObjCIdType() && !T->isObjCQualifiedIdType() && 1644 !T->isObjCClassType() && !T->isObjCQualifiedClassType()) { 1645 if (HasEmptyPlaceHolder) 1646 OS << ' '; 1647 OS << '*'; 1648 } 1649 } 1650 1651 void TypePrinter::printObjCObjectPointerAfter(const ObjCObjectPointerType *T, 1652 raw_ostream &OS) {} 1653 1654 static 1655 const TemplateArgument &getArgument(const TemplateArgument &A) { return A; } 1656 1657 static const TemplateArgument &getArgument(const TemplateArgumentLoc &A) { 1658 return A.getArgument(); 1659 } 1660 1661 static void printArgument(const TemplateArgument &A, const PrintingPolicy &PP, 1662 llvm::raw_ostream &OS) { 1663 A.print(PP, OS); 1664 } 1665 1666 static void printArgument(const TemplateArgumentLoc &A, 1667 const PrintingPolicy &PP, llvm::raw_ostream &OS) { 1668 const TemplateArgument::ArgKind &Kind = A.getArgument().getKind(); 1669 if (Kind == TemplateArgument::ArgKind::Type) 1670 return A.getTypeSourceInfo()->getType().print(OS, PP); 1671 return A.getArgument().print(PP, OS); 1672 } 1673 1674 template<typename TA> 1675 static void printTo(raw_ostream &OS, ArrayRef<TA> Args, 1676 const PrintingPolicy &Policy, bool SkipBrackets) { 1677 const char *Comma = Policy.MSVCFormatting ? "," : ", "; 1678 if (!SkipBrackets) 1679 OS << '<'; 1680 1681 bool NeedSpace = false; 1682 bool FirstArg = true; 1683 for (const auto &Arg : Args) { 1684 // Print the argument into a string. 1685 SmallString<128> Buf; 1686 llvm::raw_svector_ostream ArgOS(Buf); 1687 const TemplateArgument &Argument = getArgument(Arg); 1688 if (Argument.getKind() == TemplateArgument::Pack) { 1689 if (Argument.pack_size() && !FirstArg) 1690 OS << Comma; 1691 printTo(ArgOS, Argument.getPackAsArray(), Policy, true); 1692 } else { 1693 if (!FirstArg) 1694 OS << Comma; 1695 // Tries to print the argument with location info if exists. 1696 printArgument(Arg, Policy, ArgOS); 1697 } 1698 StringRef ArgString = ArgOS.str(); 1699 1700 // If this is the first argument and its string representation 1701 // begins with the global scope specifier ('::foo'), add a space 1702 // to avoid printing the diagraph '<:'. 1703 if (FirstArg && !ArgString.empty() && ArgString[0] == ':') 1704 OS << ' '; 1705 1706 OS << ArgString; 1707 1708 NeedSpace = (!ArgString.empty() && ArgString.back() == '>'); 1709 FirstArg = false; 1710 } 1711 1712 // If the last character of our string is '>', add another space to 1713 // keep the two '>''s separate tokens. We don't *have* to do this in 1714 // C++0x, but it's still good hygiene. 1715 if (NeedSpace) 1716 OS << ' '; 1717 1718 if (!SkipBrackets) 1719 OS << '>'; 1720 } 1721 1722 void clang::printTemplateArgumentList(raw_ostream &OS, 1723 const TemplateArgumentListInfo &Args, 1724 const PrintingPolicy &Policy) { 1725 return printTo(OS, Args.arguments(), Policy, false); 1726 } 1727 1728 void clang::printTemplateArgumentList(raw_ostream &OS, 1729 ArrayRef<TemplateArgument> Args, 1730 const PrintingPolicy &Policy) { 1731 printTo(OS, Args, Policy, false); 1732 } 1733 1734 void clang::printTemplateArgumentList(raw_ostream &OS, 1735 ArrayRef<TemplateArgumentLoc> Args, 1736 const PrintingPolicy &Policy) { 1737 printTo(OS, Args, Policy, false); 1738 } 1739 1740 std::string Qualifiers::getAsString() const { 1741 LangOptions LO; 1742 return getAsString(PrintingPolicy(LO)); 1743 } 1744 1745 // Appends qualifiers to the given string, separated by spaces. Will 1746 // prefix a space if the string is non-empty. Will not append a final 1747 // space. 1748 std::string Qualifiers::getAsString(const PrintingPolicy &Policy) const { 1749 SmallString<64> Buf; 1750 llvm::raw_svector_ostream StrOS(Buf); 1751 print(StrOS, Policy); 1752 return StrOS.str(); 1753 } 1754 1755 bool Qualifiers::isEmptyWhenPrinted(const PrintingPolicy &Policy) const { 1756 if (getCVRQualifiers()) 1757 return false; 1758 1759 if (getAddressSpace() != LangAS::Default) 1760 return false; 1761 1762 if (getObjCGCAttr()) 1763 return false; 1764 1765 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) 1766 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime)) 1767 return false; 1768 1769 return true; 1770 } 1771 1772 // Appends qualifiers to the given string, separated by spaces. Will 1773 // prefix a space if the string is non-empty. Will not append a final 1774 // space. 1775 void Qualifiers::print(raw_ostream &OS, const PrintingPolicy& Policy, 1776 bool appendSpaceIfNonEmpty) const { 1777 bool addSpace = false; 1778 1779 unsigned quals = getCVRQualifiers(); 1780 if (quals) { 1781 AppendTypeQualList(OS, quals, Policy.Restrict); 1782 addSpace = true; 1783 } 1784 if (hasUnaligned()) { 1785 if (addSpace) 1786 OS << ' '; 1787 OS << "__unaligned"; 1788 addSpace = true; 1789 } 1790 LangAS addrspace = getAddressSpace(); 1791 if (addrspace != LangAS::Default) { 1792 if (addrspace != LangAS::opencl_private) { 1793 if (addSpace) 1794 OS << ' '; 1795 addSpace = true; 1796 switch (addrspace) { 1797 case LangAS::opencl_global: 1798 OS << "__global"; 1799 break; 1800 case LangAS::opencl_local: 1801 OS << "__local"; 1802 break; 1803 case LangAS::opencl_private: 1804 break; 1805 case LangAS::opencl_constant: 1806 case LangAS::cuda_constant: 1807 OS << "__constant"; 1808 break; 1809 case LangAS::opencl_generic: 1810 OS << "__generic"; 1811 break; 1812 case LangAS::cuda_device: 1813 OS << "__device"; 1814 break; 1815 case LangAS::cuda_shared: 1816 OS << "__shared"; 1817 break; 1818 default: 1819 OS << "__attribute__((address_space("; 1820 OS << toTargetAddressSpace(addrspace); 1821 OS << ")))"; 1822 } 1823 } 1824 } 1825 if (Qualifiers::GC gc = getObjCGCAttr()) { 1826 if (addSpace) 1827 OS << ' '; 1828 addSpace = true; 1829 if (gc == Qualifiers::Weak) 1830 OS << "__weak"; 1831 else 1832 OS << "__strong"; 1833 } 1834 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) { 1835 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime)){ 1836 if (addSpace) 1837 OS << ' '; 1838 addSpace = true; 1839 } 1840 1841 switch (lifetime) { 1842 case Qualifiers::OCL_None: llvm_unreachable("none but true"); 1843 case Qualifiers::OCL_ExplicitNone: OS << "__unsafe_unretained"; break; 1844 case Qualifiers::OCL_Strong: 1845 if (!Policy.SuppressStrongLifetime) 1846 OS << "__strong"; 1847 break; 1848 1849 case Qualifiers::OCL_Weak: OS << "__weak"; break; 1850 case Qualifiers::OCL_Autoreleasing: OS << "__autoreleasing"; break; 1851 } 1852 } 1853 1854 if (appendSpaceIfNonEmpty && addSpace) 1855 OS << ' '; 1856 } 1857 1858 std::string QualType::getAsString() const { 1859 return getAsString(split(), LangOptions()); 1860 } 1861 1862 std::string QualType::getAsString(const PrintingPolicy &Policy) const { 1863 std::string S; 1864 getAsStringInternal(S, Policy); 1865 return S; 1866 } 1867 1868 std::string QualType::getAsString(const Type *ty, Qualifiers qs, 1869 const PrintingPolicy &Policy) { 1870 std::string buffer; 1871 getAsStringInternal(ty, qs, buffer, Policy); 1872 return buffer; 1873 } 1874 1875 void QualType::print(raw_ostream &OS, const PrintingPolicy &Policy, 1876 const Twine &PlaceHolder, unsigned Indentation) const { 1877 print(splitAccordingToPolicy(*this, Policy), OS, Policy, PlaceHolder, 1878 Indentation); 1879 } 1880 1881 void QualType::print(const Type *ty, Qualifiers qs, 1882 raw_ostream &OS, const PrintingPolicy &policy, 1883 const Twine &PlaceHolder, unsigned Indentation) { 1884 SmallString<128> PHBuf; 1885 StringRef PH = PlaceHolder.toStringRef(PHBuf); 1886 1887 TypePrinter(policy, Indentation).print(ty, qs, OS, PH); 1888 } 1889 1890 void QualType::getAsStringInternal(std::string &Str, 1891 const PrintingPolicy &Policy) const { 1892 return getAsStringInternal(splitAccordingToPolicy(*this, Policy), Str, 1893 Policy); 1894 } 1895 1896 void QualType::getAsStringInternal(const Type *ty, Qualifiers qs, 1897 std::string &buffer, 1898 const PrintingPolicy &policy) { 1899 SmallString<256> Buf; 1900 llvm::raw_svector_ostream StrOS(Buf); 1901 TypePrinter(policy).print(ty, qs, StrOS, buffer); 1902 std::string str = StrOS.str(); 1903 buffer.swap(str); 1904 } 1905