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