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.inc" 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.inc" 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.inc" 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 (EST_NoThrow == getExceptionSpecType()) { 738 OS << " __attribute__((nothrow))"; 739 } else if (isNoexceptExceptionSpec(getExceptionSpecType())) { 740 OS << " noexcept"; 741 // FIXME:Is it useful to print out the expression for a non-dependent 742 // noexcept specification? 743 if (isComputedNoexcept(getExceptionSpecType())) { 744 OS << '('; 745 if (getNoexceptExpr()) 746 getNoexceptExpr()->printPretty(OS, nullptr, Policy); 747 OS << ')'; 748 } 749 } 750 } 751 752 void TypePrinter::printFunctionProtoBefore(const FunctionProtoType *T, 753 raw_ostream &OS) { 754 if (T->hasTrailingReturn()) { 755 OS << "auto "; 756 if (!HasEmptyPlaceHolder) 757 OS << '('; 758 } else { 759 // If needed for precedence reasons, wrap the inner part in grouping parens. 760 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false); 761 printBefore(T->getReturnType(), OS); 762 if (!PrevPHIsEmpty.get()) 763 OS << '('; 764 } 765 } 766 767 StringRef clang::getParameterABISpelling(ParameterABI ABI) { 768 switch (ABI) { 769 case ParameterABI::Ordinary: 770 llvm_unreachable("asking for spelling of ordinary parameter ABI"); 771 case ParameterABI::SwiftContext: 772 return "swift_context"; 773 case ParameterABI::SwiftErrorResult: 774 return "swift_error_result"; 775 case ParameterABI::SwiftIndirectResult: 776 return "swift_indirect_result"; 777 } 778 llvm_unreachable("bad parameter ABI kind"); 779 } 780 781 void TypePrinter::printFunctionProtoAfter(const FunctionProtoType *T, 782 raw_ostream &OS) { 783 // If needed for precedence reasons, wrap the inner part in grouping parens. 784 if (!HasEmptyPlaceHolder) 785 OS << ')'; 786 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 787 788 OS << '('; 789 { 790 ParamPolicyRAII ParamPolicy(Policy); 791 for (unsigned i = 0, e = T->getNumParams(); i != e; ++i) { 792 if (i) OS << ", "; 793 794 auto EPI = T->getExtParameterInfo(i); 795 if (EPI.isConsumed()) OS << "__attribute__((ns_consumed)) "; 796 if (EPI.isNoEscape()) 797 OS << "__attribute__((noescape)) "; 798 auto ABI = EPI.getABI(); 799 if (ABI != ParameterABI::Ordinary) 800 OS << "__attribute__((" << getParameterABISpelling(ABI) << ")) "; 801 802 print(T->getParamType(i), OS, StringRef()); 803 } 804 } 805 806 if (T->isVariadic()) { 807 if (T->getNumParams()) 808 OS << ", "; 809 OS << "..."; 810 } else if (T->getNumParams() == 0 && Policy.UseVoidForZeroParams) { 811 // Do not emit int() if we have a proto, emit 'int(void)'. 812 OS << "void"; 813 } 814 815 OS << ')'; 816 817 FunctionType::ExtInfo Info = T->getExtInfo(); 818 819 printFunctionAfter(Info, OS); 820 821 if (!T->getMethodQuals().empty()) 822 OS << " " << T->getMethodQuals().getAsString(); 823 824 switch (T->getRefQualifier()) { 825 case RQ_None: 826 break; 827 828 case RQ_LValue: 829 OS << " &"; 830 break; 831 832 case RQ_RValue: 833 OS << " &&"; 834 break; 835 } 836 T->printExceptionSpecification(OS, Policy); 837 838 if (T->hasTrailingReturn()) { 839 OS << " -> "; 840 print(T->getReturnType(), OS, StringRef()); 841 } else 842 printAfter(T->getReturnType(), OS); 843 } 844 845 void TypePrinter::printFunctionAfter(const FunctionType::ExtInfo &Info, 846 raw_ostream &OS) { 847 if (!InsideCCAttribute) { 848 switch (Info.getCC()) { 849 case CC_C: 850 // The C calling convention is the default on the vast majority of platforms 851 // we support. If the user wrote it explicitly, it will usually be printed 852 // while traversing the AttributedType. If the type has been desugared, let 853 // the canonical spelling be the implicit calling convention. 854 // FIXME: It would be better to be explicit in certain contexts, such as a 855 // cdecl function typedef used to declare a member function with the 856 // Microsoft C++ ABI. 857 break; 858 case CC_X86StdCall: 859 OS << " __attribute__((stdcall))"; 860 break; 861 case CC_X86FastCall: 862 OS << " __attribute__((fastcall))"; 863 break; 864 case CC_X86ThisCall: 865 OS << " __attribute__((thiscall))"; 866 break; 867 case CC_X86VectorCall: 868 OS << " __attribute__((vectorcall))"; 869 break; 870 case CC_X86Pascal: 871 OS << " __attribute__((pascal))"; 872 break; 873 case CC_AAPCS: 874 OS << " __attribute__((pcs(\"aapcs\")))"; 875 break; 876 case CC_AAPCS_VFP: 877 OS << " __attribute__((pcs(\"aapcs-vfp\")))"; 878 break; 879 case CC_AArch64VectorCall: 880 OS << "__attribute__((aarch64_vector_pcs))"; 881 break; 882 case CC_IntelOclBicc: 883 OS << " __attribute__((intel_ocl_bicc))"; 884 break; 885 case CC_Win64: 886 OS << " __attribute__((ms_abi))"; 887 break; 888 case CC_X86_64SysV: 889 OS << " __attribute__((sysv_abi))"; 890 break; 891 case CC_X86RegCall: 892 OS << " __attribute__((regcall))"; 893 break; 894 case CC_SpirFunction: 895 case CC_OpenCLKernel: 896 // Do nothing. These CCs are not available as attributes. 897 break; 898 case CC_Swift: 899 OS << " __attribute__((swiftcall))"; 900 break; 901 case CC_PreserveMost: 902 OS << " __attribute__((preserve_most))"; 903 break; 904 case CC_PreserveAll: 905 OS << " __attribute__((preserve_all))"; 906 break; 907 } 908 } 909 910 if (Info.getNoReturn()) 911 OS << " __attribute__((noreturn))"; 912 if (Info.getCmseNSCall()) 913 OS << " __attribute__((cmse_nonsecure_call))"; 914 if (Info.getProducesResult()) 915 OS << " __attribute__((ns_returns_retained))"; 916 if (Info.getRegParm()) 917 OS << " __attribute__((regparm (" 918 << Info.getRegParm() << ")))"; 919 if (Info.getNoCallerSavedRegs()) 920 OS << " __attribute__((no_caller_saved_registers))"; 921 if (Info.getNoCfCheck()) 922 OS << " __attribute__((nocf_check))"; 923 } 924 925 void TypePrinter::printFunctionNoProtoBefore(const FunctionNoProtoType *T, 926 raw_ostream &OS) { 927 // If needed for precedence reasons, wrap the inner part in grouping parens. 928 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false); 929 printBefore(T->getReturnType(), OS); 930 if (!PrevPHIsEmpty.get()) 931 OS << '('; 932 } 933 934 void TypePrinter::printFunctionNoProtoAfter(const FunctionNoProtoType *T, 935 raw_ostream &OS) { 936 // If needed for precedence reasons, wrap the inner part in grouping parens. 937 if (!HasEmptyPlaceHolder) 938 OS << ')'; 939 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 940 941 OS << "()"; 942 printFunctionAfter(T->getExtInfo(), OS); 943 printAfter(T->getReturnType(), OS); 944 } 945 946 void TypePrinter::printTypeSpec(NamedDecl *D, raw_ostream &OS) { 947 948 // Compute the full nested-name-specifier for this type. 949 // In C, this will always be empty except when the type 950 // being printed is anonymous within other Record. 951 if (!Policy.SuppressScope) 952 AppendScope(D->getDeclContext(), OS); 953 954 IdentifierInfo *II = D->getIdentifier(); 955 OS << II->getName(); 956 spaceBeforePlaceHolder(OS); 957 } 958 959 void TypePrinter::printUnresolvedUsingBefore(const UnresolvedUsingType *T, 960 raw_ostream &OS) { 961 printTypeSpec(T->getDecl(), OS); 962 } 963 964 void TypePrinter::printUnresolvedUsingAfter(const UnresolvedUsingType *T, 965 raw_ostream &OS) {} 966 967 void TypePrinter::printTypedefBefore(const TypedefType *T, raw_ostream &OS) { 968 printTypeSpec(T->getDecl(), OS); 969 } 970 971 void TypePrinter::printMacroQualifiedBefore(const MacroQualifiedType *T, 972 raw_ostream &OS) { 973 StringRef MacroName = T->getMacroIdentifier()->getName(); 974 OS << MacroName << " "; 975 976 // Since this type is meant to print the macro instead of the whole attribute, 977 // we trim any attributes and go directly to the original modified type. 978 printBefore(T->getModifiedType(), OS); 979 } 980 981 void TypePrinter::printMacroQualifiedAfter(const MacroQualifiedType *T, 982 raw_ostream &OS) { 983 printAfter(T->getModifiedType(), OS); 984 } 985 986 void TypePrinter::printTypedefAfter(const TypedefType *T, raw_ostream &OS) {} 987 988 void TypePrinter::printTypeOfExprBefore(const TypeOfExprType *T, 989 raw_ostream &OS) { 990 OS << "typeof "; 991 if (T->getUnderlyingExpr()) 992 T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy); 993 spaceBeforePlaceHolder(OS); 994 } 995 996 void TypePrinter::printTypeOfExprAfter(const TypeOfExprType *T, 997 raw_ostream &OS) {} 998 999 void TypePrinter::printTypeOfBefore(const TypeOfType *T, raw_ostream &OS) { 1000 OS << "typeof("; 1001 print(T->getUnderlyingType(), OS, StringRef()); 1002 OS << ')'; 1003 spaceBeforePlaceHolder(OS); 1004 } 1005 1006 void TypePrinter::printTypeOfAfter(const TypeOfType *T, raw_ostream &OS) {} 1007 1008 void TypePrinter::printDecltypeBefore(const DecltypeType *T, raw_ostream &OS) { 1009 OS << "decltype("; 1010 if (T->getUnderlyingExpr()) 1011 T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy); 1012 OS << ')'; 1013 spaceBeforePlaceHolder(OS); 1014 } 1015 1016 void TypePrinter::printDecltypeAfter(const DecltypeType *T, raw_ostream &OS) {} 1017 1018 void TypePrinter::printUnaryTransformBefore(const UnaryTransformType *T, 1019 raw_ostream &OS) { 1020 IncludeStrongLifetimeRAII Strong(Policy); 1021 1022 switch (T->getUTTKind()) { 1023 case UnaryTransformType::EnumUnderlyingType: 1024 OS << "__underlying_type("; 1025 print(T->getBaseType(), OS, StringRef()); 1026 OS << ')'; 1027 spaceBeforePlaceHolder(OS); 1028 return; 1029 } 1030 1031 printBefore(T->getBaseType(), OS); 1032 } 1033 1034 void TypePrinter::printUnaryTransformAfter(const UnaryTransformType *T, 1035 raw_ostream &OS) { 1036 IncludeStrongLifetimeRAII Strong(Policy); 1037 1038 switch (T->getUTTKind()) { 1039 case UnaryTransformType::EnumUnderlyingType: 1040 return; 1041 } 1042 1043 printAfter(T->getBaseType(), OS); 1044 } 1045 1046 void TypePrinter::printAutoBefore(const AutoType *T, raw_ostream &OS) { 1047 // If the type has been deduced, do not print 'auto'. 1048 if (!T->getDeducedType().isNull()) { 1049 printBefore(T->getDeducedType(), OS); 1050 } else { 1051 if (T->isConstrained()) { 1052 OS << T->getTypeConstraintConcept()->getName(); 1053 auto Args = T->getTypeConstraintArguments(); 1054 if (!Args.empty()) 1055 printTemplateArgumentList(OS, Args, Policy); 1056 OS << ' '; 1057 } 1058 switch (T->getKeyword()) { 1059 case AutoTypeKeyword::Auto: OS << "auto"; break; 1060 case AutoTypeKeyword::DecltypeAuto: OS << "decltype(auto)"; break; 1061 case AutoTypeKeyword::GNUAutoType: OS << "__auto_type"; break; 1062 } 1063 spaceBeforePlaceHolder(OS); 1064 } 1065 } 1066 1067 void TypePrinter::printAutoAfter(const AutoType *T, raw_ostream &OS) { 1068 // If the type has been deduced, do not print 'auto'. 1069 if (!T->getDeducedType().isNull()) 1070 printAfter(T->getDeducedType(), OS); 1071 } 1072 1073 void TypePrinter::printDeducedTemplateSpecializationBefore( 1074 const DeducedTemplateSpecializationType *T, raw_ostream &OS) { 1075 // If the type has been deduced, print the deduced type. 1076 if (!T->getDeducedType().isNull()) { 1077 printBefore(T->getDeducedType(), OS); 1078 } else { 1079 IncludeStrongLifetimeRAII Strong(Policy); 1080 T->getTemplateName().print(OS, Policy); 1081 spaceBeforePlaceHolder(OS); 1082 } 1083 } 1084 1085 void TypePrinter::printDeducedTemplateSpecializationAfter( 1086 const DeducedTemplateSpecializationType *T, raw_ostream &OS) { 1087 // If the type has been deduced, print the deduced type. 1088 if (!T->getDeducedType().isNull()) 1089 printAfter(T->getDeducedType(), OS); 1090 } 1091 1092 void TypePrinter::printAtomicBefore(const AtomicType *T, raw_ostream &OS) { 1093 IncludeStrongLifetimeRAII Strong(Policy); 1094 1095 OS << "_Atomic("; 1096 print(T->getValueType(), OS, StringRef()); 1097 OS << ')'; 1098 spaceBeforePlaceHolder(OS); 1099 } 1100 1101 void TypePrinter::printAtomicAfter(const AtomicType *T, raw_ostream &OS) {} 1102 1103 void TypePrinter::printPipeBefore(const PipeType *T, raw_ostream &OS) { 1104 IncludeStrongLifetimeRAII Strong(Policy); 1105 1106 if (T->isReadOnly()) 1107 OS << "read_only "; 1108 else 1109 OS << "write_only "; 1110 OS << "pipe "; 1111 print(T->getElementType(), OS, StringRef()); 1112 spaceBeforePlaceHolder(OS); 1113 } 1114 1115 void TypePrinter::printPipeAfter(const PipeType *T, raw_ostream &OS) {} 1116 1117 /// Appends the given scope to the end of a string. 1118 void TypePrinter::AppendScope(DeclContext *DC, raw_ostream &OS) { 1119 if (DC->isTranslationUnit()) return; 1120 if (DC->isFunctionOrMethod()) return; 1121 AppendScope(DC->getParent(), OS); 1122 1123 if (const auto *NS = dyn_cast<NamespaceDecl>(DC)) { 1124 if (Policy.SuppressUnwrittenScope && 1125 (NS->isAnonymousNamespace() || NS->isInline())) 1126 return; 1127 if (NS->getIdentifier()) 1128 OS << NS->getName() << "::"; 1129 else 1130 OS << "(anonymous namespace)::"; 1131 } else if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(DC)) { 1132 IncludeStrongLifetimeRAII Strong(Policy); 1133 OS << Spec->getIdentifier()->getName(); 1134 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 1135 printTemplateArgumentList(OS, TemplateArgs.asArray(), Policy); 1136 OS << "::"; 1137 } else if (const auto *Tag = dyn_cast<TagDecl>(DC)) { 1138 if (TypedefNameDecl *Typedef = Tag->getTypedefNameForAnonDecl()) 1139 OS << Typedef->getIdentifier()->getName() << "::"; 1140 else if (Tag->getIdentifier()) 1141 OS << Tag->getIdentifier()->getName() << "::"; 1142 else 1143 return; 1144 } 1145 } 1146 1147 void TypePrinter::printTag(TagDecl *D, raw_ostream &OS) { 1148 if (Policy.IncludeTagDefinition) { 1149 PrintingPolicy SubPolicy = Policy; 1150 SubPolicy.IncludeTagDefinition = false; 1151 D->print(OS, SubPolicy, Indentation); 1152 spaceBeforePlaceHolder(OS); 1153 return; 1154 } 1155 1156 bool HasKindDecoration = false; 1157 1158 // We don't print tags unless this is an elaborated type. 1159 // In C, we just assume every RecordType is an elaborated type. 1160 if (!Policy.SuppressTagKeyword && !D->getTypedefNameForAnonDecl()) { 1161 HasKindDecoration = true; 1162 OS << D->getKindName(); 1163 OS << ' '; 1164 } 1165 1166 // Compute the full nested-name-specifier for this type. 1167 // In C, this will always be empty except when the type 1168 // being printed is anonymous within other Record. 1169 if (!Policy.SuppressScope) 1170 AppendScope(D->getDeclContext(), OS); 1171 1172 if (const IdentifierInfo *II = D->getIdentifier()) 1173 OS << II->getName(); 1174 else if (TypedefNameDecl *Typedef = D->getTypedefNameForAnonDecl()) { 1175 assert(Typedef->getIdentifier() && "Typedef without identifier?"); 1176 OS << Typedef->getIdentifier()->getName(); 1177 } else { 1178 // Make an unambiguous representation for anonymous types, e.g. 1179 // (anonymous enum at /usr/include/string.h:120:9) 1180 OS << (Policy.MSVCFormatting ? '`' : '('); 1181 1182 if (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda()) { 1183 OS << "lambda"; 1184 HasKindDecoration = true; 1185 } else { 1186 OS << "anonymous"; 1187 } 1188 1189 if (Policy.AnonymousTagLocations) { 1190 // Suppress the redundant tag keyword if we just printed one. 1191 // We don't have to worry about ElaboratedTypes here because you can't 1192 // refer to an anonymous type with one. 1193 if (!HasKindDecoration) 1194 OS << " " << D->getKindName(); 1195 1196 PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc( 1197 D->getLocation()); 1198 if (PLoc.isValid()) { 1199 OS << " at "; 1200 StringRef File = PLoc.getFilename(); 1201 if (auto *Callbacks = Policy.Callbacks) 1202 OS << Callbacks->remapPath(File); 1203 else 1204 OS << File; 1205 OS << ':' << PLoc.getLine() << ':' << PLoc.getColumn(); 1206 } 1207 } 1208 1209 OS << (Policy.MSVCFormatting ? '\'' : ')'); 1210 } 1211 1212 // If this is a class template specialization, print the template 1213 // arguments. 1214 if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(D)) { 1215 ArrayRef<TemplateArgument> Args; 1216 TypeSourceInfo *TAW = Spec->getTypeAsWritten(); 1217 if (!Policy.PrintCanonicalTypes && TAW) { 1218 const TemplateSpecializationType *TST = 1219 cast<TemplateSpecializationType>(TAW->getType()); 1220 Args = TST->template_arguments(); 1221 } else { 1222 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 1223 Args = TemplateArgs.asArray(); 1224 } 1225 IncludeStrongLifetimeRAII Strong(Policy); 1226 printTemplateArgumentList(OS, Args, Policy); 1227 } 1228 1229 spaceBeforePlaceHolder(OS); 1230 } 1231 1232 void TypePrinter::printRecordBefore(const RecordType *T, raw_ostream &OS) { 1233 printTag(T->getDecl(), OS); 1234 } 1235 1236 void TypePrinter::printRecordAfter(const RecordType *T, raw_ostream &OS) {} 1237 1238 void TypePrinter::printEnumBefore(const EnumType *T, raw_ostream &OS) { 1239 printTag(T->getDecl(), OS); 1240 } 1241 1242 void TypePrinter::printEnumAfter(const EnumType *T, raw_ostream &OS) {} 1243 1244 void TypePrinter::printTemplateTypeParmBefore(const TemplateTypeParmType *T, 1245 raw_ostream &OS) { 1246 TemplateTypeParmDecl *D = T->getDecl(); 1247 if (D && D->isImplicit()) { 1248 if (auto *TC = D->getTypeConstraint()) { 1249 TC->print(OS, Policy); 1250 OS << ' '; 1251 } 1252 OS << "auto"; 1253 } else if (IdentifierInfo *Id = T->getIdentifier()) 1254 OS << Id->getName(); 1255 else 1256 OS << "type-parameter-" << T->getDepth() << '-' << T->getIndex(); 1257 1258 spaceBeforePlaceHolder(OS); 1259 } 1260 1261 void TypePrinter::printTemplateTypeParmAfter(const TemplateTypeParmType *T, 1262 raw_ostream &OS) {} 1263 1264 void TypePrinter::printSubstTemplateTypeParmBefore( 1265 const SubstTemplateTypeParmType *T, 1266 raw_ostream &OS) { 1267 IncludeStrongLifetimeRAII Strong(Policy); 1268 printBefore(T->getReplacementType(), OS); 1269 } 1270 1271 void TypePrinter::printSubstTemplateTypeParmAfter( 1272 const SubstTemplateTypeParmType *T, 1273 raw_ostream &OS) { 1274 IncludeStrongLifetimeRAII Strong(Policy); 1275 printAfter(T->getReplacementType(), OS); 1276 } 1277 1278 void TypePrinter::printSubstTemplateTypeParmPackBefore( 1279 const SubstTemplateTypeParmPackType *T, 1280 raw_ostream &OS) { 1281 IncludeStrongLifetimeRAII Strong(Policy); 1282 printTemplateTypeParmBefore(T->getReplacedParameter(), OS); 1283 } 1284 1285 void TypePrinter::printSubstTemplateTypeParmPackAfter( 1286 const SubstTemplateTypeParmPackType *T, 1287 raw_ostream &OS) { 1288 IncludeStrongLifetimeRAII Strong(Policy); 1289 printTemplateTypeParmAfter(T->getReplacedParameter(), OS); 1290 } 1291 1292 void TypePrinter::printTemplateSpecializationBefore( 1293 const TemplateSpecializationType *T, 1294 raw_ostream &OS) { 1295 IncludeStrongLifetimeRAII Strong(Policy); 1296 T->getTemplateName().print(OS, Policy); 1297 1298 printTemplateArgumentList(OS, T->template_arguments(), Policy); 1299 spaceBeforePlaceHolder(OS); 1300 } 1301 1302 void TypePrinter::printTemplateSpecializationAfter( 1303 const TemplateSpecializationType *T, 1304 raw_ostream &OS) {} 1305 1306 void TypePrinter::printInjectedClassNameBefore(const InjectedClassNameType *T, 1307 raw_ostream &OS) { 1308 printTemplateSpecializationBefore(T->getInjectedTST(), OS); 1309 } 1310 1311 void TypePrinter::printInjectedClassNameAfter(const InjectedClassNameType *T, 1312 raw_ostream &OS) {} 1313 1314 void TypePrinter::printElaboratedBefore(const ElaboratedType *T, 1315 raw_ostream &OS) { 1316 if (Policy.IncludeTagDefinition && T->getOwnedTagDecl()) { 1317 TagDecl *OwnedTagDecl = T->getOwnedTagDecl(); 1318 assert(OwnedTagDecl->getTypeForDecl() == T->getNamedType().getTypePtr() && 1319 "OwnedTagDecl expected to be a declaration for the type"); 1320 PrintingPolicy SubPolicy = Policy; 1321 SubPolicy.IncludeTagDefinition = false; 1322 OwnedTagDecl->print(OS, SubPolicy, Indentation); 1323 spaceBeforePlaceHolder(OS); 1324 return; 1325 } 1326 1327 // The tag definition will take care of these. 1328 if (!Policy.IncludeTagDefinition) 1329 { 1330 OS << TypeWithKeyword::getKeywordName(T->getKeyword()); 1331 if (T->getKeyword() != ETK_None) 1332 OS << " "; 1333 NestedNameSpecifier *Qualifier = T->getQualifier(); 1334 if (Qualifier) 1335 Qualifier->print(OS, Policy); 1336 } 1337 1338 ElaboratedTypePolicyRAII PolicyRAII(Policy); 1339 printBefore(T->getNamedType(), OS); 1340 } 1341 1342 void TypePrinter::printElaboratedAfter(const ElaboratedType *T, 1343 raw_ostream &OS) { 1344 if (Policy.IncludeTagDefinition && T->getOwnedTagDecl()) 1345 return; 1346 ElaboratedTypePolicyRAII PolicyRAII(Policy); 1347 printAfter(T->getNamedType(), OS); 1348 } 1349 1350 void TypePrinter::printParenBefore(const ParenType *T, raw_ostream &OS) { 1351 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) { 1352 printBefore(T->getInnerType(), OS); 1353 OS << '('; 1354 } else 1355 printBefore(T->getInnerType(), OS); 1356 } 1357 1358 void TypePrinter::printParenAfter(const ParenType *T, raw_ostream &OS) { 1359 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) { 1360 OS << ')'; 1361 printAfter(T->getInnerType(), OS); 1362 } else 1363 printAfter(T->getInnerType(), OS); 1364 } 1365 1366 void TypePrinter::printDependentNameBefore(const DependentNameType *T, 1367 raw_ostream &OS) { 1368 OS << TypeWithKeyword::getKeywordName(T->getKeyword()); 1369 if (T->getKeyword() != ETK_None) 1370 OS << " "; 1371 1372 T->getQualifier()->print(OS, Policy); 1373 1374 OS << T->getIdentifier()->getName(); 1375 spaceBeforePlaceHolder(OS); 1376 } 1377 1378 void TypePrinter::printDependentNameAfter(const DependentNameType *T, 1379 raw_ostream &OS) {} 1380 1381 void TypePrinter::printDependentTemplateSpecializationBefore( 1382 const DependentTemplateSpecializationType *T, raw_ostream &OS) { 1383 IncludeStrongLifetimeRAII Strong(Policy); 1384 1385 OS << TypeWithKeyword::getKeywordName(T->getKeyword()); 1386 if (T->getKeyword() != ETK_None) 1387 OS << " "; 1388 1389 if (T->getQualifier()) 1390 T->getQualifier()->print(OS, Policy); 1391 OS << T->getIdentifier()->getName(); 1392 printTemplateArgumentList(OS, T->template_arguments(), Policy); 1393 spaceBeforePlaceHolder(OS); 1394 } 1395 1396 void TypePrinter::printDependentTemplateSpecializationAfter( 1397 const DependentTemplateSpecializationType *T, raw_ostream &OS) {} 1398 1399 void TypePrinter::printPackExpansionBefore(const PackExpansionType *T, 1400 raw_ostream &OS) { 1401 printBefore(T->getPattern(), OS); 1402 } 1403 1404 void TypePrinter::printPackExpansionAfter(const PackExpansionType *T, 1405 raw_ostream &OS) { 1406 printAfter(T->getPattern(), OS); 1407 OS << "..."; 1408 } 1409 1410 void TypePrinter::printAttributedBefore(const AttributedType *T, 1411 raw_ostream &OS) { 1412 // FIXME: Generate this with TableGen. 1413 1414 // Prefer the macro forms of the GC and ownership qualifiers. 1415 if (T->getAttrKind() == attr::ObjCGC || 1416 T->getAttrKind() == attr::ObjCOwnership) 1417 return printBefore(T->getEquivalentType(), OS); 1418 1419 if (T->getAttrKind() == attr::ObjCKindOf) 1420 OS << "__kindof "; 1421 1422 if (T->getAttrKind() == attr::AddressSpace) 1423 printBefore(T->getEquivalentType(), OS); 1424 else 1425 printBefore(T->getModifiedType(), OS); 1426 1427 if (T->isMSTypeSpec()) { 1428 switch (T->getAttrKind()) { 1429 default: return; 1430 case attr::Ptr32: OS << " __ptr32"; break; 1431 case attr::Ptr64: OS << " __ptr64"; break; 1432 case attr::SPtr: OS << " __sptr"; break; 1433 case attr::UPtr: OS << " __uptr"; break; 1434 } 1435 spaceBeforePlaceHolder(OS); 1436 } 1437 1438 // Print nullability type specifiers. 1439 if (T->getImmediateNullability()) { 1440 if (T->getAttrKind() == attr::TypeNonNull) 1441 OS << " _Nonnull"; 1442 else if (T->getAttrKind() == attr::TypeNullable) 1443 OS << " _Nullable"; 1444 else if (T->getAttrKind() == attr::TypeNullUnspecified) 1445 OS << " _Null_unspecified"; 1446 else 1447 llvm_unreachable("unhandled nullability"); 1448 spaceBeforePlaceHolder(OS); 1449 } 1450 } 1451 1452 void TypePrinter::printAttributedAfter(const AttributedType *T, 1453 raw_ostream &OS) { 1454 // FIXME: Generate this with TableGen. 1455 1456 // Prefer the macro forms of the GC and ownership qualifiers. 1457 if (T->getAttrKind() == attr::ObjCGC || 1458 T->getAttrKind() == attr::ObjCOwnership) 1459 return printAfter(T->getEquivalentType(), OS); 1460 1461 // If this is a calling convention attribute, don't print the implicit CC from 1462 // the modified type. 1463 SaveAndRestore<bool> MaybeSuppressCC(InsideCCAttribute, T->isCallingConv()); 1464 1465 printAfter(T->getModifiedType(), OS); 1466 1467 // Some attributes are printed as qualifiers before the type, so we have 1468 // nothing left to do. 1469 if (T->getAttrKind() == attr::ObjCKindOf || 1470 T->isMSTypeSpec() || T->getImmediateNullability()) 1471 return; 1472 1473 // Don't print the inert __unsafe_unretained attribute at all. 1474 if (T->getAttrKind() == attr::ObjCInertUnsafeUnretained) 1475 return; 1476 1477 // Don't print ns_returns_retained unless it had an effect. 1478 if (T->getAttrKind() == attr::NSReturnsRetained && 1479 !T->getEquivalentType()->castAs<FunctionType>() 1480 ->getExtInfo().getProducesResult()) 1481 return; 1482 1483 if (T->getAttrKind() == attr::LifetimeBound) { 1484 OS << " [[clang::lifetimebound]]"; 1485 return; 1486 } 1487 1488 // The printing of the address_space attribute is handled by the qualifier 1489 // since it is still stored in the qualifier. Return early to prevent printing 1490 // this twice. 1491 if (T->getAttrKind() == attr::AddressSpace) 1492 return; 1493 1494 OS << " __attribute__(("; 1495 switch (T->getAttrKind()) { 1496 #define TYPE_ATTR(NAME) 1497 #define DECL_OR_TYPE_ATTR(NAME) 1498 #define ATTR(NAME) case attr::NAME: 1499 #include "clang/Basic/AttrList.inc" 1500 llvm_unreachable("non-type attribute attached to type"); 1501 1502 case attr::OpenCLPrivateAddressSpace: 1503 case attr::OpenCLGlobalAddressSpace: 1504 case attr::OpenCLLocalAddressSpace: 1505 case attr::OpenCLConstantAddressSpace: 1506 case attr::OpenCLGenericAddressSpace: 1507 // FIXME: Update printAttributedBefore to print these once we generate 1508 // AttributedType nodes for them. 1509 break; 1510 1511 case attr::LifetimeBound: 1512 case attr::TypeNonNull: 1513 case attr::TypeNullable: 1514 case attr::TypeNullUnspecified: 1515 case attr::ObjCGC: 1516 case attr::ObjCInertUnsafeUnretained: 1517 case attr::ObjCKindOf: 1518 case attr::ObjCOwnership: 1519 case attr::Ptr32: 1520 case attr::Ptr64: 1521 case attr::SPtr: 1522 case attr::UPtr: 1523 case attr::AddressSpace: 1524 case attr::CmseNSCall: 1525 llvm_unreachable("This attribute should have been handled already"); 1526 1527 case attr::NSReturnsRetained: 1528 OS << "ns_returns_retained"; 1529 break; 1530 1531 // FIXME: When Sema learns to form this AttributedType, avoid printing the 1532 // attribute again in printFunctionProtoAfter. 1533 case attr::AnyX86NoCfCheck: OS << "nocf_check"; break; 1534 case attr::CDecl: OS << "cdecl"; break; 1535 case attr::FastCall: OS << "fastcall"; break; 1536 case attr::StdCall: OS << "stdcall"; break; 1537 case attr::ThisCall: OS << "thiscall"; break; 1538 case attr::SwiftCall: OS << "swiftcall"; break; 1539 case attr::VectorCall: OS << "vectorcall"; break; 1540 case attr::Pascal: OS << "pascal"; break; 1541 case attr::MSABI: OS << "ms_abi"; break; 1542 case attr::SysVABI: OS << "sysv_abi"; break; 1543 case attr::RegCall: OS << "regcall"; break; 1544 case attr::Pcs: { 1545 OS << "pcs("; 1546 QualType t = T->getEquivalentType(); 1547 while (!t->isFunctionType()) 1548 t = t->getPointeeType(); 1549 OS << (t->castAs<FunctionType>()->getCallConv() == CC_AAPCS ? 1550 "\"aapcs\"" : "\"aapcs-vfp\""); 1551 OS << ')'; 1552 break; 1553 } 1554 case attr::AArch64VectorPcs: OS << "aarch64_vector_pcs"; break; 1555 case attr::IntelOclBicc: OS << "inteloclbicc"; break; 1556 case attr::PreserveMost: 1557 OS << "preserve_most"; 1558 break; 1559 1560 case attr::PreserveAll: 1561 OS << "preserve_all"; 1562 break; 1563 case attr::NoDeref: 1564 OS << "noderef"; 1565 break; 1566 case attr::AcquireHandle: 1567 OS << "acquire_handle"; 1568 break; 1569 case attr::ArmMveStrictPolymorphism: 1570 OS << "__clang_arm_mve_strict_polymorphism"; 1571 break; 1572 } 1573 OS << "))"; 1574 } 1575 1576 void TypePrinter::printObjCInterfaceBefore(const ObjCInterfaceType *T, 1577 raw_ostream &OS) { 1578 OS << T->getDecl()->getName(); 1579 spaceBeforePlaceHolder(OS); 1580 } 1581 1582 void TypePrinter::printObjCInterfaceAfter(const ObjCInterfaceType *T, 1583 raw_ostream &OS) {} 1584 1585 void TypePrinter::printObjCTypeParamBefore(const ObjCTypeParamType *T, 1586 raw_ostream &OS) { 1587 OS << T->getDecl()->getName(); 1588 if (!T->qual_empty()) { 1589 bool isFirst = true; 1590 OS << '<'; 1591 for (const auto *I : T->quals()) { 1592 if (isFirst) 1593 isFirst = false; 1594 else 1595 OS << ','; 1596 OS << I->getName(); 1597 } 1598 OS << '>'; 1599 } 1600 1601 spaceBeforePlaceHolder(OS); 1602 } 1603 1604 void TypePrinter::printObjCTypeParamAfter(const ObjCTypeParamType *T, 1605 raw_ostream &OS) {} 1606 1607 void TypePrinter::printObjCObjectBefore(const ObjCObjectType *T, 1608 raw_ostream &OS) { 1609 if (T->qual_empty() && T->isUnspecializedAsWritten() && 1610 !T->isKindOfTypeAsWritten()) 1611 return printBefore(T->getBaseType(), OS); 1612 1613 if (T->isKindOfTypeAsWritten()) 1614 OS << "__kindof "; 1615 1616 print(T->getBaseType(), OS, StringRef()); 1617 1618 if (T->isSpecializedAsWritten()) { 1619 bool isFirst = true; 1620 OS << '<'; 1621 for (auto typeArg : T->getTypeArgsAsWritten()) { 1622 if (isFirst) 1623 isFirst = false; 1624 else 1625 OS << ","; 1626 1627 print(typeArg, OS, StringRef()); 1628 } 1629 OS << '>'; 1630 } 1631 1632 if (!T->qual_empty()) { 1633 bool isFirst = true; 1634 OS << '<'; 1635 for (const auto *I : T->quals()) { 1636 if (isFirst) 1637 isFirst = false; 1638 else 1639 OS << ','; 1640 OS << I->getName(); 1641 } 1642 OS << '>'; 1643 } 1644 1645 spaceBeforePlaceHolder(OS); 1646 } 1647 1648 void TypePrinter::printObjCObjectAfter(const ObjCObjectType *T, 1649 raw_ostream &OS) { 1650 if (T->qual_empty() && T->isUnspecializedAsWritten() && 1651 !T->isKindOfTypeAsWritten()) 1652 return printAfter(T->getBaseType(), OS); 1653 } 1654 1655 void TypePrinter::printObjCObjectPointerBefore(const ObjCObjectPointerType *T, 1656 raw_ostream &OS) { 1657 printBefore(T->getPointeeType(), OS); 1658 1659 // If we need to print the pointer, print it now. 1660 if (!T->isObjCIdType() && !T->isObjCQualifiedIdType() && 1661 !T->isObjCClassType() && !T->isObjCQualifiedClassType()) { 1662 if (HasEmptyPlaceHolder) 1663 OS << ' '; 1664 OS << '*'; 1665 } 1666 } 1667 1668 void TypePrinter::printObjCObjectPointerAfter(const ObjCObjectPointerType *T, 1669 raw_ostream &OS) {} 1670 1671 static 1672 const TemplateArgument &getArgument(const TemplateArgument &A) { return A; } 1673 1674 static const TemplateArgument &getArgument(const TemplateArgumentLoc &A) { 1675 return A.getArgument(); 1676 } 1677 1678 static void printArgument(const TemplateArgument &A, const PrintingPolicy &PP, 1679 llvm::raw_ostream &OS) { 1680 A.print(PP, OS); 1681 } 1682 1683 static void printArgument(const TemplateArgumentLoc &A, 1684 const PrintingPolicy &PP, llvm::raw_ostream &OS) { 1685 const TemplateArgument::ArgKind &Kind = A.getArgument().getKind(); 1686 if (Kind == TemplateArgument::ArgKind::Type) 1687 return A.getTypeSourceInfo()->getType().print(OS, PP); 1688 return A.getArgument().print(PP, OS); 1689 } 1690 1691 template<typename TA> 1692 static void printTo(raw_ostream &OS, ArrayRef<TA> Args, 1693 const PrintingPolicy &Policy, bool SkipBrackets) { 1694 const char *Comma = Policy.MSVCFormatting ? "," : ", "; 1695 if (!SkipBrackets) 1696 OS << '<'; 1697 1698 bool NeedSpace = false; 1699 bool FirstArg = true; 1700 for (const auto &Arg : Args) { 1701 // Print the argument into a string. 1702 SmallString<128> Buf; 1703 llvm::raw_svector_ostream ArgOS(Buf); 1704 const TemplateArgument &Argument = getArgument(Arg); 1705 if (Argument.getKind() == TemplateArgument::Pack) { 1706 if (Argument.pack_size() && !FirstArg) 1707 OS << Comma; 1708 printTo(ArgOS, Argument.getPackAsArray(), Policy, true); 1709 } else { 1710 if (!FirstArg) 1711 OS << Comma; 1712 // Tries to print the argument with location info if exists. 1713 printArgument(Arg, Policy, ArgOS); 1714 } 1715 StringRef ArgString = ArgOS.str(); 1716 1717 // If this is the first argument and its string representation 1718 // begins with the global scope specifier ('::foo'), add a space 1719 // to avoid printing the diagraph '<:'. 1720 if (FirstArg && !ArgString.empty() && ArgString[0] == ':') 1721 OS << ' '; 1722 1723 OS << ArgString; 1724 1725 // If the last character of our string is '>', add another space to 1726 // keep the two '>''s separate tokens. 1727 NeedSpace = Policy.SplitTemplateClosers && !ArgString.empty() && 1728 ArgString.back() == '>'; 1729 FirstArg = false; 1730 } 1731 1732 if (NeedSpace) 1733 OS << ' '; 1734 1735 if (!SkipBrackets) 1736 OS << '>'; 1737 } 1738 1739 void clang::printTemplateArgumentList(raw_ostream &OS, 1740 const TemplateArgumentListInfo &Args, 1741 const PrintingPolicy &Policy) { 1742 return printTo(OS, Args.arguments(), Policy, false); 1743 } 1744 1745 void clang::printTemplateArgumentList(raw_ostream &OS, 1746 ArrayRef<TemplateArgument> Args, 1747 const PrintingPolicy &Policy) { 1748 printTo(OS, Args, Policy, false); 1749 } 1750 1751 void clang::printTemplateArgumentList(raw_ostream &OS, 1752 ArrayRef<TemplateArgumentLoc> Args, 1753 const PrintingPolicy &Policy) { 1754 printTo(OS, Args, Policy, false); 1755 } 1756 1757 std::string Qualifiers::getAsString() const { 1758 LangOptions LO; 1759 return getAsString(PrintingPolicy(LO)); 1760 } 1761 1762 // Appends qualifiers to the given string, separated by spaces. Will 1763 // prefix a space if the string is non-empty. Will not append a final 1764 // space. 1765 std::string Qualifiers::getAsString(const PrintingPolicy &Policy) const { 1766 SmallString<64> Buf; 1767 llvm::raw_svector_ostream StrOS(Buf); 1768 print(StrOS, Policy); 1769 return std::string(StrOS.str()); 1770 } 1771 1772 bool Qualifiers::isEmptyWhenPrinted(const PrintingPolicy &Policy) const { 1773 if (getCVRQualifiers()) 1774 return false; 1775 1776 if (getAddressSpace() != LangAS::Default) 1777 return false; 1778 1779 if (getObjCGCAttr()) 1780 return false; 1781 1782 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) 1783 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime)) 1784 return false; 1785 1786 return true; 1787 } 1788 1789 std::string Qualifiers::getAddrSpaceAsString(LangAS AS) { 1790 switch (AS) { 1791 case LangAS::Default: 1792 return ""; 1793 case LangAS::opencl_global: 1794 return "__global"; 1795 case LangAS::opencl_local: 1796 return "__local"; 1797 case LangAS::opencl_private: 1798 return "__private"; 1799 case LangAS::opencl_constant: 1800 return "__constant"; 1801 case LangAS::opencl_generic: 1802 return "__generic"; 1803 case LangAS::cuda_device: 1804 return "__device__"; 1805 case LangAS::cuda_constant: 1806 return "__constant__"; 1807 case LangAS::cuda_shared: 1808 return "__shared__"; 1809 case LangAS::ptr32_sptr: 1810 return "__sptr __ptr32"; 1811 case LangAS::ptr32_uptr: 1812 return "__uptr __ptr32"; 1813 case LangAS::ptr64: 1814 return "__ptr64"; 1815 default: 1816 return std::to_string(toTargetAddressSpace(AS)); 1817 } 1818 } 1819 1820 // Appends qualifiers to the given string, separated by spaces. Will 1821 // prefix a space if the string is non-empty. Will not append a final 1822 // space. 1823 void Qualifiers::print(raw_ostream &OS, const PrintingPolicy& Policy, 1824 bool appendSpaceIfNonEmpty) const { 1825 bool addSpace = false; 1826 1827 unsigned quals = getCVRQualifiers(); 1828 if (quals) { 1829 AppendTypeQualList(OS, quals, Policy.Restrict); 1830 addSpace = true; 1831 } 1832 if (hasUnaligned()) { 1833 if (addSpace) 1834 OS << ' '; 1835 OS << "__unaligned"; 1836 addSpace = true; 1837 } 1838 auto ASStr = getAddrSpaceAsString(getAddressSpace()); 1839 if (!ASStr.empty()) { 1840 if (addSpace) 1841 OS << ' '; 1842 addSpace = true; 1843 // Wrap target address space into an attribute syntax 1844 if (isTargetAddressSpace(getAddressSpace())) 1845 OS << "__attribute__((address_space(" << ASStr << ")))"; 1846 else 1847 OS << ASStr; 1848 } 1849 1850 if (Qualifiers::GC gc = getObjCGCAttr()) { 1851 if (addSpace) 1852 OS << ' '; 1853 addSpace = true; 1854 if (gc == Qualifiers::Weak) 1855 OS << "__weak"; 1856 else 1857 OS << "__strong"; 1858 } 1859 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) { 1860 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime)){ 1861 if (addSpace) 1862 OS << ' '; 1863 addSpace = true; 1864 } 1865 1866 switch (lifetime) { 1867 case Qualifiers::OCL_None: llvm_unreachable("none but true"); 1868 case Qualifiers::OCL_ExplicitNone: OS << "__unsafe_unretained"; break; 1869 case Qualifiers::OCL_Strong: 1870 if (!Policy.SuppressStrongLifetime) 1871 OS << "__strong"; 1872 break; 1873 1874 case Qualifiers::OCL_Weak: OS << "__weak"; break; 1875 case Qualifiers::OCL_Autoreleasing: OS << "__autoreleasing"; break; 1876 } 1877 } 1878 1879 if (appendSpaceIfNonEmpty && addSpace) 1880 OS << ' '; 1881 } 1882 1883 std::string QualType::getAsString() const { 1884 return getAsString(split(), LangOptions()); 1885 } 1886 1887 std::string QualType::getAsString(const PrintingPolicy &Policy) const { 1888 std::string S; 1889 getAsStringInternal(S, Policy); 1890 return S; 1891 } 1892 1893 std::string QualType::getAsString(const Type *ty, Qualifiers qs, 1894 const PrintingPolicy &Policy) { 1895 std::string buffer; 1896 getAsStringInternal(ty, qs, buffer, Policy); 1897 return buffer; 1898 } 1899 1900 void QualType::print(raw_ostream &OS, const PrintingPolicy &Policy, 1901 const Twine &PlaceHolder, unsigned Indentation) const { 1902 print(splitAccordingToPolicy(*this, Policy), OS, Policy, PlaceHolder, 1903 Indentation); 1904 } 1905 1906 void QualType::print(const Type *ty, Qualifiers qs, 1907 raw_ostream &OS, const PrintingPolicy &policy, 1908 const Twine &PlaceHolder, unsigned Indentation) { 1909 SmallString<128> PHBuf; 1910 StringRef PH = PlaceHolder.toStringRef(PHBuf); 1911 1912 TypePrinter(policy, Indentation).print(ty, qs, OS, PH); 1913 } 1914 1915 void QualType::getAsStringInternal(std::string &Str, 1916 const PrintingPolicy &Policy) const { 1917 return getAsStringInternal(splitAccordingToPolicy(*this, Policy), Str, 1918 Policy); 1919 } 1920 1921 void QualType::getAsStringInternal(const Type *ty, Qualifiers qs, 1922 std::string &buffer, 1923 const PrintingPolicy &policy) { 1924 SmallString<256> Buf; 1925 llvm::raw_svector_ostream StrOS(Buf); 1926 TypePrinter(policy).print(ty, qs, StrOS, buffer); 1927 std::string str = std::string(StrOS.str()); 1928 buffer.swap(str); 1929 } 1930