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