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