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