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 printTemplateSpecializationBefore(T->getInjectedTST(), OS); 1333 } 1334 1335 void TypePrinter::printInjectedClassNameAfter(const InjectedClassNameType *T, 1336 raw_ostream &OS) {} 1337 1338 void TypePrinter::printElaboratedBefore(const ElaboratedType *T, 1339 raw_ostream &OS) { 1340 if (Policy.IncludeTagDefinition && T->getOwnedTagDecl()) { 1341 TagDecl *OwnedTagDecl = T->getOwnedTagDecl(); 1342 assert(OwnedTagDecl->getTypeForDecl() == T->getNamedType().getTypePtr() && 1343 "OwnedTagDecl expected to be a declaration for the type"); 1344 PrintingPolicy SubPolicy = Policy; 1345 SubPolicy.IncludeTagDefinition = false; 1346 OwnedTagDecl->print(OS, SubPolicy, Indentation); 1347 spaceBeforePlaceHolder(OS); 1348 return; 1349 } 1350 1351 // The tag definition will take care of these. 1352 if (!Policy.IncludeTagDefinition) 1353 { 1354 OS << TypeWithKeyword::getKeywordName(T->getKeyword()); 1355 if (T->getKeyword() != ETK_None) 1356 OS << " "; 1357 NestedNameSpecifier *Qualifier = T->getQualifier(); 1358 if (Qualifier) 1359 Qualifier->print(OS, Policy); 1360 } 1361 1362 ElaboratedTypePolicyRAII PolicyRAII(Policy); 1363 printBefore(T->getNamedType(), OS); 1364 } 1365 1366 void TypePrinter::printElaboratedAfter(const ElaboratedType *T, 1367 raw_ostream &OS) { 1368 if (Policy.IncludeTagDefinition && T->getOwnedTagDecl()) 1369 return; 1370 ElaboratedTypePolicyRAII PolicyRAII(Policy); 1371 printAfter(T->getNamedType(), OS); 1372 } 1373 1374 void TypePrinter::printParenBefore(const ParenType *T, raw_ostream &OS) { 1375 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) { 1376 printBefore(T->getInnerType(), OS); 1377 OS << '('; 1378 } else 1379 printBefore(T->getInnerType(), OS); 1380 } 1381 1382 void TypePrinter::printParenAfter(const ParenType *T, raw_ostream &OS) { 1383 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) { 1384 OS << ')'; 1385 printAfter(T->getInnerType(), OS); 1386 } else 1387 printAfter(T->getInnerType(), OS); 1388 } 1389 1390 void TypePrinter::printDependentNameBefore(const DependentNameType *T, 1391 raw_ostream &OS) { 1392 OS << TypeWithKeyword::getKeywordName(T->getKeyword()); 1393 if (T->getKeyword() != ETK_None) 1394 OS << " "; 1395 1396 T->getQualifier()->print(OS, Policy); 1397 1398 OS << T->getIdentifier()->getName(); 1399 spaceBeforePlaceHolder(OS); 1400 } 1401 1402 void TypePrinter::printDependentNameAfter(const DependentNameType *T, 1403 raw_ostream &OS) {} 1404 1405 void TypePrinter::printDependentTemplateSpecializationBefore( 1406 const DependentTemplateSpecializationType *T, raw_ostream &OS) { 1407 IncludeStrongLifetimeRAII Strong(Policy); 1408 1409 OS << TypeWithKeyword::getKeywordName(T->getKeyword()); 1410 if (T->getKeyword() != ETK_None) 1411 OS << " "; 1412 1413 if (T->getQualifier()) 1414 T->getQualifier()->print(OS, Policy); 1415 OS << "template " << T->getIdentifier()->getName(); 1416 printTemplateArgumentList(OS, T->template_arguments(), Policy); 1417 spaceBeforePlaceHolder(OS); 1418 } 1419 1420 void TypePrinter::printDependentTemplateSpecializationAfter( 1421 const DependentTemplateSpecializationType *T, raw_ostream &OS) {} 1422 1423 void TypePrinter::printPackExpansionBefore(const PackExpansionType *T, 1424 raw_ostream &OS) { 1425 printBefore(T->getPattern(), OS); 1426 } 1427 1428 void TypePrinter::printPackExpansionAfter(const PackExpansionType *T, 1429 raw_ostream &OS) { 1430 printAfter(T->getPattern(), OS); 1431 OS << "..."; 1432 } 1433 1434 void TypePrinter::printAttributedBefore(const AttributedType *T, 1435 raw_ostream &OS) { 1436 // FIXME: Generate this with TableGen. 1437 1438 // Prefer the macro forms of the GC and ownership qualifiers. 1439 if (T->getAttrKind() == attr::ObjCGC || 1440 T->getAttrKind() == attr::ObjCOwnership) 1441 return printBefore(T->getEquivalentType(), OS); 1442 1443 if (T->getAttrKind() == attr::ObjCKindOf) 1444 OS << "__kindof "; 1445 1446 if (T->getAttrKind() == attr::AddressSpace) 1447 printBefore(T->getEquivalentType(), OS); 1448 else 1449 printBefore(T->getModifiedType(), OS); 1450 1451 if (T->isMSTypeSpec()) { 1452 switch (T->getAttrKind()) { 1453 default: return; 1454 case attr::Ptr32: OS << " __ptr32"; break; 1455 case attr::Ptr64: OS << " __ptr64"; break; 1456 case attr::SPtr: OS << " __sptr"; break; 1457 case attr::UPtr: OS << " __uptr"; break; 1458 } 1459 spaceBeforePlaceHolder(OS); 1460 } 1461 1462 // Print nullability type specifiers. 1463 if (T->getImmediateNullability()) { 1464 if (T->getAttrKind() == attr::TypeNonNull) 1465 OS << " _Nonnull"; 1466 else if (T->getAttrKind() == attr::TypeNullable) 1467 OS << " _Nullable"; 1468 else if (T->getAttrKind() == attr::TypeNullUnspecified) 1469 OS << " _Null_unspecified"; 1470 else 1471 llvm_unreachable("unhandled nullability"); 1472 spaceBeforePlaceHolder(OS); 1473 } 1474 } 1475 1476 void TypePrinter::printAttributedAfter(const AttributedType *T, 1477 raw_ostream &OS) { 1478 // FIXME: Generate this with TableGen. 1479 1480 // Prefer the macro forms of the GC and ownership qualifiers. 1481 if (T->getAttrKind() == attr::ObjCGC || 1482 T->getAttrKind() == attr::ObjCOwnership) 1483 return printAfter(T->getEquivalentType(), OS); 1484 1485 // If this is a calling convention attribute, don't print the implicit CC from 1486 // the modified type. 1487 SaveAndRestore<bool> MaybeSuppressCC(InsideCCAttribute, T->isCallingConv()); 1488 1489 printAfter(T->getModifiedType(), OS); 1490 1491 // Some attributes are printed as qualifiers before the type, so we have 1492 // nothing left to do. 1493 if (T->getAttrKind() == attr::ObjCKindOf || 1494 T->isMSTypeSpec() || T->getImmediateNullability()) 1495 return; 1496 1497 // Don't print the inert __unsafe_unretained attribute at all. 1498 if (T->getAttrKind() == attr::ObjCInertUnsafeUnretained) 1499 return; 1500 1501 // Don't print ns_returns_retained unless it had an effect. 1502 if (T->getAttrKind() == attr::NSReturnsRetained && 1503 !T->getEquivalentType()->castAs<FunctionType>() 1504 ->getExtInfo().getProducesResult()) 1505 return; 1506 1507 if (T->getAttrKind() == attr::LifetimeBound) { 1508 OS << " [[clang::lifetimebound]]"; 1509 return; 1510 } 1511 1512 // The printing of the address_space attribute is handled by the qualifier 1513 // since it is still stored in the qualifier. Return early to prevent printing 1514 // this twice. 1515 if (T->getAttrKind() == attr::AddressSpace) 1516 return; 1517 1518 OS << " __attribute__(("; 1519 switch (T->getAttrKind()) { 1520 #define TYPE_ATTR(NAME) 1521 #define DECL_OR_TYPE_ATTR(NAME) 1522 #define ATTR(NAME) case attr::NAME: 1523 #include "clang/Basic/AttrList.inc" 1524 llvm_unreachable("non-type attribute attached to type"); 1525 1526 case attr::OpenCLPrivateAddressSpace: 1527 case attr::OpenCLGlobalAddressSpace: 1528 case attr::OpenCLLocalAddressSpace: 1529 case attr::OpenCLConstantAddressSpace: 1530 case attr::OpenCLGenericAddressSpace: 1531 // FIXME: Update printAttributedBefore to print these once we generate 1532 // AttributedType nodes for them. 1533 break; 1534 1535 case attr::LifetimeBound: 1536 case attr::TypeNonNull: 1537 case attr::TypeNullable: 1538 case attr::TypeNullUnspecified: 1539 case attr::ObjCGC: 1540 case attr::ObjCInertUnsafeUnretained: 1541 case attr::ObjCKindOf: 1542 case attr::ObjCOwnership: 1543 case attr::Ptr32: 1544 case attr::Ptr64: 1545 case attr::SPtr: 1546 case attr::UPtr: 1547 case attr::AddressSpace: 1548 case attr::CmseNSCall: 1549 llvm_unreachable("This attribute should have been handled already"); 1550 1551 case attr::NSReturnsRetained: 1552 OS << "ns_returns_retained"; 1553 break; 1554 1555 // FIXME: When Sema learns to form this AttributedType, avoid printing the 1556 // attribute again in printFunctionProtoAfter. 1557 case attr::AnyX86NoCfCheck: OS << "nocf_check"; break; 1558 case attr::CDecl: OS << "cdecl"; break; 1559 case attr::FastCall: OS << "fastcall"; break; 1560 case attr::StdCall: OS << "stdcall"; break; 1561 case attr::ThisCall: OS << "thiscall"; break; 1562 case attr::SwiftCall: OS << "swiftcall"; break; 1563 case attr::VectorCall: OS << "vectorcall"; break; 1564 case attr::Pascal: OS << "pascal"; break; 1565 case attr::MSABI: OS << "ms_abi"; break; 1566 case attr::SysVABI: OS << "sysv_abi"; break; 1567 case attr::RegCall: OS << "regcall"; break; 1568 case attr::Pcs: { 1569 OS << "pcs("; 1570 QualType t = T->getEquivalentType(); 1571 while (!t->isFunctionType()) 1572 t = t->getPointeeType(); 1573 OS << (t->castAs<FunctionType>()->getCallConv() == CC_AAPCS ? 1574 "\"aapcs\"" : "\"aapcs-vfp\""); 1575 OS << ')'; 1576 break; 1577 } 1578 case attr::AArch64VectorPcs: OS << "aarch64_vector_pcs"; break; 1579 case attr::IntelOclBicc: OS << "inteloclbicc"; break; 1580 case attr::PreserveMost: 1581 OS << "preserve_most"; 1582 break; 1583 1584 case attr::PreserveAll: 1585 OS << "preserve_all"; 1586 break; 1587 case attr::NoDeref: 1588 OS << "noderef"; 1589 break; 1590 case attr::AcquireHandle: 1591 OS << "acquire_handle"; 1592 break; 1593 case attr::ArmMveStrictPolymorphism: 1594 OS << "__clang_arm_mve_strict_polymorphism"; 1595 break; 1596 } 1597 OS << "))"; 1598 } 1599 1600 void TypePrinter::printObjCInterfaceBefore(const ObjCInterfaceType *T, 1601 raw_ostream &OS) { 1602 OS << T->getDecl()->getName(); 1603 spaceBeforePlaceHolder(OS); 1604 } 1605 1606 void TypePrinter::printObjCInterfaceAfter(const ObjCInterfaceType *T, 1607 raw_ostream &OS) {} 1608 1609 void TypePrinter::printObjCTypeParamBefore(const ObjCTypeParamType *T, 1610 raw_ostream &OS) { 1611 OS << T->getDecl()->getName(); 1612 if (!T->qual_empty()) { 1613 bool isFirst = true; 1614 OS << '<'; 1615 for (const auto *I : T->quals()) { 1616 if (isFirst) 1617 isFirst = false; 1618 else 1619 OS << ','; 1620 OS << I->getName(); 1621 } 1622 OS << '>'; 1623 } 1624 1625 spaceBeforePlaceHolder(OS); 1626 } 1627 1628 void TypePrinter::printObjCTypeParamAfter(const ObjCTypeParamType *T, 1629 raw_ostream &OS) {} 1630 1631 void TypePrinter::printObjCObjectBefore(const ObjCObjectType *T, 1632 raw_ostream &OS) { 1633 if (T->qual_empty() && T->isUnspecializedAsWritten() && 1634 !T->isKindOfTypeAsWritten()) 1635 return printBefore(T->getBaseType(), OS); 1636 1637 if (T->isKindOfTypeAsWritten()) 1638 OS << "__kindof "; 1639 1640 print(T->getBaseType(), OS, StringRef()); 1641 1642 if (T->isSpecializedAsWritten()) { 1643 bool isFirst = true; 1644 OS << '<'; 1645 for (auto typeArg : T->getTypeArgsAsWritten()) { 1646 if (isFirst) 1647 isFirst = false; 1648 else 1649 OS << ","; 1650 1651 print(typeArg, OS, StringRef()); 1652 } 1653 OS << '>'; 1654 } 1655 1656 if (!T->qual_empty()) { 1657 bool isFirst = true; 1658 OS << '<'; 1659 for (const auto *I : T->quals()) { 1660 if (isFirst) 1661 isFirst = false; 1662 else 1663 OS << ','; 1664 OS << I->getName(); 1665 } 1666 OS << '>'; 1667 } 1668 1669 spaceBeforePlaceHolder(OS); 1670 } 1671 1672 void TypePrinter::printObjCObjectAfter(const ObjCObjectType *T, 1673 raw_ostream &OS) { 1674 if (T->qual_empty() && T->isUnspecializedAsWritten() && 1675 !T->isKindOfTypeAsWritten()) 1676 return printAfter(T->getBaseType(), OS); 1677 } 1678 1679 void TypePrinter::printObjCObjectPointerBefore(const ObjCObjectPointerType *T, 1680 raw_ostream &OS) { 1681 printBefore(T->getPointeeType(), OS); 1682 1683 // If we need to print the pointer, print it now. 1684 if (!T->isObjCIdType() && !T->isObjCQualifiedIdType() && 1685 !T->isObjCClassType() && !T->isObjCQualifiedClassType()) { 1686 if (HasEmptyPlaceHolder) 1687 OS << ' '; 1688 OS << '*'; 1689 } 1690 } 1691 1692 void TypePrinter::printObjCObjectPointerAfter(const ObjCObjectPointerType *T, 1693 raw_ostream &OS) {} 1694 1695 static 1696 const TemplateArgument &getArgument(const TemplateArgument &A) { return A; } 1697 1698 static const TemplateArgument &getArgument(const TemplateArgumentLoc &A) { 1699 return A.getArgument(); 1700 } 1701 1702 static void printArgument(const TemplateArgument &A, const PrintingPolicy &PP, 1703 llvm::raw_ostream &OS) { 1704 A.print(PP, OS); 1705 } 1706 1707 static void printArgument(const TemplateArgumentLoc &A, 1708 const PrintingPolicy &PP, llvm::raw_ostream &OS) { 1709 const TemplateArgument::ArgKind &Kind = A.getArgument().getKind(); 1710 if (Kind == TemplateArgument::ArgKind::Type) 1711 return A.getTypeSourceInfo()->getType().print(OS, PP); 1712 return A.getArgument().print(PP, OS); 1713 } 1714 1715 template<typename TA> 1716 static void printTo(raw_ostream &OS, ArrayRef<TA> Args, 1717 const PrintingPolicy &Policy, bool SkipBrackets) { 1718 const char *Comma = Policy.MSVCFormatting ? "," : ", "; 1719 if (!SkipBrackets) 1720 OS << '<'; 1721 1722 bool NeedSpace = false; 1723 bool FirstArg = true; 1724 for (const auto &Arg : Args) { 1725 // Print the argument into a string. 1726 SmallString<128> Buf; 1727 llvm::raw_svector_ostream ArgOS(Buf); 1728 const TemplateArgument &Argument = getArgument(Arg); 1729 if (Argument.getKind() == TemplateArgument::Pack) { 1730 if (Argument.pack_size() && !FirstArg) 1731 OS << Comma; 1732 printTo(ArgOS, Argument.getPackAsArray(), Policy, true); 1733 } else { 1734 if (!FirstArg) 1735 OS << Comma; 1736 // Tries to print the argument with location info if exists. 1737 printArgument(Arg, Policy, ArgOS); 1738 } 1739 StringRef ArgString = ArgOS.str(); 1740 1741 // If this is the first argument and its string representation 1742 // begins with the global scope specifier ('::foo'), add a space 1743 // to avoid printing the diagraph '<:'. 1744 if (FirstArg && !ArgString.empty() && ArgString[0] == ':') 1745 OS << ' '; 1746 1747 OS << ArgString; 1748 1749 // If the last character of our string is '>', add another space to 1750 // keep the two '>''s separate tokens. 1751 NeedSpace = Policy.SplitTemplateClosers && !ArgString.empty() && 1752 ArgString.back() == '>'; 1753 FirstArg = false; 1754 } 1755 1756 if (NeedSpace) 1757 OS << ' '; 1758 1759 if (!SkipBrackets) 1760 OS << '>'; 1761 } 1762 1763 void clang::printTemplateArgumentList(raw_ostream &OS, 1764 const TemplateArgumentListInfo &Args, 1765 const PrintingPolicy &Policy) { 1766 return printTo(OS, Args.arguments(), Policy, false); 1767 } 1768 1769 void clang::printTemplateArgumentList(raw_ostream &OS, 1770 ArrayRef<TemplateArgument> Args, 1771 const PrintingPolicy &Policy) { 1772 printTo(OS, Args, Policy, false); 1773 } 1774 1775 void clang::printTemplateArgumentList(raw_ostream &OS, 1776 ArrayRef<TemplateArgumentLoc> Args, 1777 const PrintingPolicy &Policy) { 1778 printTo(OS, Args, Policy, false); 1779 } 1780 1781 std::string Qualifiers::getAsString() const { 1782 LangOptions LO; 1783 return getAsString(PrintingPolicy(LO)); 1784 } 1785 1786 // Appends qualifiers to the given string, separated by spaces. Will 1787 // prefix a space if the string is non-empty. Will not append a final 1788 // space. 1789 std::string Qualifiers::getAsString(const PrintingPolicy &Policy) const { 1790 SmallString<64> Buf; 1791 llvm::raw_svector_ostream StrOS(Buf); 1792 print(StrOS, Policy); 1793 return std::string(StrOS.str()); 1794 } 1795 1796 bool Qualifiers::isEmptyWhenPrinted(const PrintingPolicy &Policy) const { 1797 if (getCVRQualifiers()) 1798 return false; 1799 1800 if (getAddressSpace() != LangAS::Default) 1801 return false; 1802 1803 if (getObjCGCAttr()) 1804 return false; 1805 1806 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) 1807 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime)) 1808 return false; 1809 1810 return true; 1811 } 1812 1813 std::string Qualifiers::getAddrSpaceAsString(LangAS AS) { 1814 switch (AS) { 1815 case LangAS::Default: 1816 return ""; 1817 case LangAS::opencl_global: 1818 return "__global"; 1819 case LangAS::opencl_local: 1820 return "__local"; 1821 case LangAS::opencl_private: 1822 return "__private"; 1823 case LangAS::opencl_constant: 1824 return "__constant"; 1825 case LangAS::opencl_generic: 1826 return "__generic"; 1827 case LangAS::cuda_device: 1828 return "__device__"; 1829 case LangAS::cuda_constant: 1830 return "__constant__"; 1831 case LangAS::cuda_shared: 1832 return "__shared__"; 1833 case LangAS::ptr32_sptr: 1834 return "__sptr __ptr32"; 1835 case LangAS::ptr32_uptr: 1836 return "__uptr __ptr32"; 1837 case LangAS::ptr64: 1838 return "__ptr64"; 1839 default: 1840 return std::to_string(toTargetAddressSpace(AS)); 1841 } 1842 } 1843 1844 // Appends qualifiers to the given string, separated by spaces. Will 1845 // prefix a space if the string is non-empty. Will not append a final 1846 // space. 1847 void Qualifiers::print(raw_ostream &OS, const PrintingPolicy& Policy, 1848 bool appendSpaceIfNonEmpty) const { 1849 bool addSpace = false; 1850 1851 unsigned quals = getCVRQualifiers(); 1852 if (quals) { 1853 AppendTypeQualList(OS, quals, Policy.Restrict); 1854 addSpace = true; 1855 } 1856 if (hasUnaligned()) { 1857 if (addSpace) 1858 OS << ' '; 1859 OS << "__unaligned"; 1860 addSpace = true; 1861 } 1862 auto ASStr = getAddrSpaceAsString(getAddressSpace()); 1863 if (!ASStr.empty()) { 1864 if (addSpace) 1865 OS << ' '; 1866 addSpace = true; 1867 // Wrap target address space into an attribute syntax 1868 if (isTargetAddressSpace(getAddressSpace())) 1869 OS << "__attribute__((address_space(" << ASStr << ")))"; 1870 else 1871 OS << ASStr; 1872 } 1873 1874 if (Qualifiers::GC gc = getObjCGCAttr()) { 1875 if (addSpace) 1876 OS << ' '; 1877 addSpace = true; 1878 if (gc == Qualifiers::Weak) 1879 OS << "__weak"; 1880 else 1881 OS << "__strong"; 1882 } 1883 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) { 1884 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime)){ 1885 if (addSpace) 1886 OS << ' '; 1887 addSpace = true; 1888 } 1889 1890 switch (lifetime) { 1891 case Qualifiers::OCL_None: llvm_unreachable("none but true"); 1892 case Qualifiers::OCL_ExplicitNone: OS << "__unsafe_unretained"; break; 1893 case Qualifiers::OCL_Strong: 1894 if (!Policy.SuppressStrongLifetime) 1895 OS << "__strong"; 1896 break; 1897 1898 case Qualifiers::OCL_Weak: OS << "__weak"; break; 1899 case Qualifiers::OCL_Autoreleasing: OS << "__autoreleasing"; break; 1900 } 1901 } 1902 1903 if (appendSpaceIfNonEmpty && addSpace) 1904 OS << ' '; 1905 } 1906 1907 std::string QualType::getAsString() const { 1908 return getAsString(split(), LangOptions()); 1909 } 1910 1911 std::string QualType::getAsString(const PrintingPolicy &Policy) const { 1912 std::string S; 1913 getAsStringInternal(S, Policy); 1914 return S; 1915 } 1916 1917 std::string QualType::getAsString(const Type *ty, Qualifiers qs, 1918 const PrintingPolicy &Policy) { 1919 std::string buffer; 1920 getAsStringInternal(ty, qs, buffer, Policy); 1921 return buffer; 1922 } 1923 1924 void QualType::print(raw_ostream &OS, const PrintingPolicy &Policy, 1925 const Twine &PlaceHolder, unsigned Indentation) const { 1926 print(splitAccordingToPolicy(*this, Policy), OS, Policy, PlaceHolder, 1927 Indentation); 1928 } 1929 1930 void QualType::print(const Type *ty, Qualifiers qs, 1931 raw_ostream &OS, const PrintingPolicy &policy, 1932 const Twine &PlaceHolder, unsigned Indentation) { 1933 SmallString<128> PHBuf; 1934 StringRef PH = PlaceHolder.toStringRef(PHBuf); 1935 1936 TypePrinter(policy, Indentation).print(ty, qs, OS, PH); 1937 } 1938 1939 void QualType::getAsStringInternal(std::string &Str, 1940 const PrintingPolicy &Policy) const { 1941 return getAsStringInternal(splitAccordingToPolicy(*this, Policy), Str, 1942 Policy); 1943 } 1944 1945 void QualType::getAsStringInternal(const Type *ty, Qualifiers qs, 1946 std::string &buffer, 1947 const PrintingPolicy &policy) { 1948 SmallString<256> Buf; 1949 llvm::raw_svector_ostream StrOS(Buf); 1950 TypePrinter(policy).print(ty, qs, StrOS, buffer); 1951 std::string str = std::string(StrOS.str()); 1952 buffer.swap(str); 1953 } 1954