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_X86RegCall: 729 OS << " __attribute__((regcall))"; 730 break; 731 case CC_SpirFunction: 732 case CC_OpenCLKernel: 733 // Do nothing. These CCs are not available as attributes. 734 break; 735 case CC_Swift: 736 OS << " __attribute__((swiftcall))"; 737 break; 738 case CC_PreserveMost: 739 OS << " __attribute__((preserve_most))"; 740 break; 741 case CC_PreserveAll: 742 OS << " __attribute__((preserve_all))"; 743 break; 744 } 745 } 746 747 if (Info.getNoReturn()) 748 OS << " __attribute__((noreturn))"; 749 if (Info.getRegParm()) 750 OS << " __attribute__((regparm (" 751 << Info.getRegParm() << ")))"; 752 753 if (unsigned quals = T->getTypeQuals()) { 754 OS << ' '; 755 AppendTypeQualList(OS, quals, Policy.Restrict); 756 } 757 758 switch (T->getRefQualifier()) { 759 case RQ_None: 760 break; 761 762 case RQ_LValue: 763 OS << " &"; 764 break; 765 766 case RQ_RValue: 767 OS << " &&"; 768 break; 769 } 770 T->printExceptionSpecification(OS, Policy); 771 772 if (T->hasTrailingReturn()) { 773 OS << " -> "; 774 print(T->getReturnType(), OS, StringRef()); 775 } else 776 printAfter(T->getReturnType(), OS); 777 } 778 779 void TypePrinter::printFunctionNoProtoBefore(const FunctionNoProtoType *T, 780 raw_ostream &OS) { 781 // If needed for precedence reasons, wrap the inner part in grouping parens. 782 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false); 783 printBefore(T->getReturnType(), OS); 784 if (!PrevPHIsEmpty.get()) 785 OS << '('; 786 } 787 void TypePrinter::printFunctionNoProtoAfter(const FunctionNoProtoType *T, 788 raw_ostream &OS) { 789 // If needed for precedence reasons, wrap the inner part in grouping parens. 790 if (!HasEmptyPlaceHolder) 791 OS << ')'; 792 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 793 794 OS << "()"; 795 if (T->getNoReturnAttr()) 796 OS << " __attribute__((noreturn))"; 797 printAfter(T->getReturnType(), OS); 798 } 799 800 void TypePrinter::printTypeSpec(const NamedDecl *D, raw_ostream &OS) { 801 IdentifierInfo *II = D->getIdentifier(); 802 OS << II->getName(); 803 spaceBeforePlaceHolder(OS); 804 } 805 806 void TypePrinter::printUnresolvedUsingBefore(const UnresolvedUsingType *T, 807 raw_ostream &OS) { 808 printTypeSpec(T->getDecl(), OS); 809 } 810 void TypePrinter::printUnresolvedUsingAfter(const UnresolvedUsingType *T, 811 raw_ostream &OS) { } 812 813 void TypePrinter::printTypedefBefore(const TypedefType *T, raw_ostream &OS) { 814 printTypeSpec(T->getDecl(), OS); 815 } 816 void TypePrinter::printTypedefAfter(const TypedefType *T, raw_ostream &OS) { } 817 818 void TypePrinter::printTypeOfExprBefore(const TypeOfExprType *T, 819 raw_ostream &OS) { 820 OS << "typeof "; 821 if (T->getUnderlyingExpr()) 822 T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy); 823 spaceBeforePlaceHolder(OS); 824 } 825 void TypePrinter::printTypeOfExprAfter(const TypeOfExprType *T, 826 raw_ostream &OS) { } 827 828 void TypePrinter::printTypeOfBefore(const TypeOfType *T, raw_ostream &OS) { 829 OS << "typeof("; 830 print(T->getUnderlyingType(), OS, StringRef()); 831 OS << ')'; 832 spaceBeforePlaceHolder(OS); 833 } 834 void TypePrinter::printTypeOfAfter(const TypeOfType *T, raw_ostream &OS) { } 835 836 void TypePrinter::printDecltypeBefore(const DecltypeType *T, raw_ostream &OS) { 837 OS << "decltype("; 838 if (T->getUnderlyingExpr()) 839 T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy); 840 OS << ')'; 841 spaceBeforePlaceHolder(OS); 842 } 843 void TypePrinter::printDecltypeAfter(const DecltypeType *T, raw_ostream &OS) { } 844 845 void TypePrinter::printUnaryTransformBefore(const UnaryTransformType *T, 846 raw_ostream &OS) { 847 IncludeStrongLifetimeRAII Strong(Policy); 848 849 switch (T->getUTTKind()) { 850 case UnaryTransformType::EnumUnderlyingType: 851 OS << "__underlying_type("; 852 print(T->getBaseType(), OS, StringRef()); 853 OS << ')'; 854 spaceBeforePlaceHolder(OS); 855 return; 856 } 857 858 printBefore(T->getBaseType(), OS); 859 } 860 void TypePrinter::printUnaryTransformAfter(const UnaryTransformType *T, 861 raw_ostream &OS) { 862 IncludeStrongLifetimeRAII Strong(Policy); 863 864 switch (T->getUTTKind()) { 865 case UnaryTransformType::EnumUnderlyingType: 866 return; 867 } 868 869 printAfter(T->getBaseType(), OS); 870 } 871 872 void TypePrinter::printAutoBefore(const AutoType *T, raw_ostream &OS) { 873 // If the type has been deduced, do not print 'auto'. 874 if (!T->getDeducedType().isNull()) { 875 printBefore(T->getDeducedType(), OS); 876 } else { 877 switch (T->getKeyword()) { 878 case AutoTypeKeyword::Auto: OS << "auto"; break; 879 case AutoTypeKeyword::DecltypeAuto: OS << "decltype(auto)"; break; 880 case AutoTypeKeyword::GNUAutoType: OS << "__auto_type"; break; 881 } 882 spaceBeforePlaceHolder(OS); 883 } 884 } 885 void TypePrinter::printAutoAfter(const AutoType *T, raw_ostream &OS) { 886 // If the type has been deduced, do not print 'auto'. 887 if (!T->getDeducedType().isNull()) 888 printAfter(T->getDeducedType(), OS); 889 } 890 891 void TypePrinter::printAtomicBefore(const AtomicType *T, raw_ostream &OS) { 892 IncludeStrongLifetimeRAII Strong(Policy); 893 894 OS << "_Atomic("; 895 print(T->getValueType(), OS, StringRef()); 896 OS << ')'; 897 spaceBeforePlaceHolder(OS); 898 } 899 void TypePrinter::printAtomicAfter(const AtomicType *T, raw_ostream &OS) { } 900 901 void TypePrinter::printPipeBefore(const PipeType *T, raw_ostream &OS) { 902 IncludeStrongLifetimeRAII Strong(Policy); 903 904 if (T->isReadOnly()) 905 OS << "read_only "; 906 else 907 OS << "write_only "; 908 OS << "pipe "; 909 print(T->getElementType(), OS, StringRef()); 910 spaceBeforePlaceHolder(OS); 911 } 912 913 void TypePrinter::printPipeAfter(const PipeType *T, raw_ostream &OS) { 914 } 915 /// Appends the given scope to the end of a string. 916 void TypePrinter::AppendScope(DeclContext *DC, raw_ostream &OS) { 917 if (DC->isTranslationUnit()) return; 918 if (DC->isFunctionOrMethod()) return; 919 AppendScope(DC->getParent(), OS); 920 921 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) { 922 if (Policy.SuppressUnwrittenScope && 923 (NS->isAnonymousNamespace() || NS->isInline())) 924 return; 925 if (NS->getIdentifier()) 926 OS << NS->getName() << "::"; 927 else 928 OS << "(anonymous namespace)::"; 929 } else if (ClassTemplateSpecializationDecl *Spec 930 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) { 931 IncludeStrongLifetimeRAII Strong(Policy); 932 OS << Spec->getIdentifier()->getName(); 933 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 934 TemplateSpecializationType::PrintTemplateArgumentList( 935 OS, TemplateArgs.asArray(), Policy); 936 OS << "::"; 937 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) { 938 if (TypedefNameDecl *Typedef = Tag->getTypedefNameForAnonDecl()) 939 OS << Typedef->getIdentifier()->getName() << "::"; 940 else if (Tag->getIdentifier()) 941 OS << Tag->getIdentifier()->getName() << "::"; 942 else 943 return; 944 } 945 } 946 947 void TypePrinter::printTag(TagDecl *D, raw_ostream &OS) { 948 if (Policy.IncludeTagDefinition) { 949 PrintingPolicy SubPolicy = Policy; 950 SubPolicy.IncludeTagDefinition = false; 951 D->print(OS, SubPolicy, Indentation); 952 spaceBeforePlaceHolder(OS); 953 return; 954 } 955 956 bool HasKindDecoration = false; 957 958 // We don't print tags unless this is an elaborated type. 959 // In C, we just assume every RecordType is an elaborated type. 960 if (!Policy.SuppressTagKeyword && !D->getTypedefNameForAnonDecl()) { 961 HasKindDecoration = true; 962 OS << D->getKindName(); 963 OS << ' '; 964 } 965 966 // Compute the full nested-name-specifier for this type. 967 // In C, this will always be empty except when the type 968 // being printed is anonymous within other Record. 969 if (!Policy.SuppressScope) 970 AppendScope(D->getDeclContext(), OS); 971 972 if (const IdentifierInfo *II = D->getIdentifier()) 973 OS << II->getName(); 974 else if (TypedefNameDecl *Typedef = D->getTypedefNameForAnonDecl()) { 975 assert(Typedef->getIdentifier() && "Typedef without identifier?"); 976 OS << Typedef->getIdentifier()->getName(); 977 } else { 978 // Make an unambiguous representation for anonymous types, e.g. 979 // (anonymous enum at /usr/include/string.h:120:9) 980 OS << (Policy.MSVCFormatting ? '`' : '('); 981 982 if (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda()) { 983 OS << "lambda"; 984 HasKindDecoration = true; 985 } else { 986 OS << "anonymous"; 987 } 988 989 if (Policy.AnonymousTagLocations) { 990 // Suppress the redundant tag keyword if we just printed one. 991 // We don't have to worry about ElaboratedTypes here because you can't 992 // refer to an anonymous type with one. 993 if (!HasKindDecoration) 994 OS << " " << D->getKindName(); 995 996 PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc( 997 D->getLocation()); 998 if (PLoc.isValid()) { 999 OS << " at " << PLoc.getFilename() 1000 << ':' << PLoc.getLine() 1001 << ':' << PLoc.getColumn(); 1002 } 1003 } 1004 1005 OS << (Policy.MSVCFormatting ? '\'' : ')'); 1006 } 1007 1008 // If this is a class template specialization, print the template 1009 // arguments. 1010 if (ClassTemplateSpecializationDecl *Spec 1011 = dyn_cast<ClassTemplateSpecializationDecl>(D)) { 1012 ArrayRef<TemplateArgument> Args; 1013 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) { 1014 const TemplateSpecializationType *TST = 1015 cast<TemplateSpecializationType>(TAW->getType()); 1016 Args = TST->template_arguments(); 1017 } else { 1018 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 1019 Args = TemplateArgs.asArray(); 1020 } 1021 IncludeStrongLifetimeRAII Strong(Policy); 1022 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, Policy); 1023 } 1024 1025 spaceBeforePlaceHolder(OS); 1026 } 1027 1028 void TypePrinter::printRecordBefore(const RecordType *T, raw_ostream &OS) { 1029 printTag(T->getDecl(), OS); 1030 } 1031 void TypePrinter::printRecordAfter(const RecordType *T, raw_ostream &OS) { } 1032 1033 void TypePrinter::printEnumBefore(const EnumType *T, raw_ostream &OS) { 1034 printTag(T->getDecl(), OS); 1035 } 1036 void TypePrinter::printEnumAfter(const EnumType *T, raw_ostream &OS) { } 1037 1038 void TypePrinter::printTemplateTypeParmBefore(const TemplateTypeParmType *T, 1039 raw_ostream &OS) { 1040 if (IdentifierInfo *Id = T->getIdentifier()) 1041 OS << Id->getName(); 1042 else 1043 OS << "type-parameter-" << T->getDepth() << '-' << T->getIndex(); 1044 spaceBeforePlaceHolder(OS); 1045 } 1046 void TypePrinter::printTemplateTypeParmAfter(const TemplateTypeParmType *T, 1047 raw_ostream &OS) { } 1048 1049 void TypePrinter::printSubstTemplateTypeParmBefore( 1050 const SubstTemplateTypeParmType *T, 1051 raw_ostream &OS) { 1052 IncludeStrongLifetimeRAII Strong(Policy); 1053 printBefore(T->getReplacementType(), OS); 1054 } 1055 void TypePrinter::printSubstTemplateTypeParmAfter( 1056 const SubstTemplateTypeParmType *T, 1057 raw_ostream &OS) { 1058 IncludeStrongLifetimeRAII Strong(Policy); 1059 printAfter(T->getReplacementType(), OS); 1060 } 1061 1062 void TypePrinter::printSubstTemplateTypeParmPackBefore( 1063 const SubstTemplateTypeParmPackType *T, 1064 raw_ostream &OS) { 1065 IncludeStrongLifetimeRAII Strong(Policy); 1066 printTemplateTypeParmBefore(T->getReplacedParameter(), OS); 1067 } 1068 void TypePrinter::printSubstTemplateTypeParmPackAfter( 1069 const SubstTemplateTypeParmPackType *T, 1070 raw_ostream &OS) { 1071 IncludeStrongLifetimeRAII Strong(Policy); 1072 printTemplateTypeParmAfter(T->getReplacedParameter(), OS); 1073 } 1074 1075 void TypePrinter::printTemplateSpecializationBefore( 1076 const TemplateSpecializationType *T, 1077 raw_ostream &OS) { 1078 IncludeStrongLifetimeRAII Strong(Policy); 1079 T->getTemplateName().print(OS, Policy); 1080 1081 TemplateSpecializationType::PrintTemplateArgumentList( 1082 OS, T->template_arguments(), Policy); 1083 spaceBeforePlaceHolder(OS); 1084 } 1085 void TypePrinter::printTemplateSpecializationAfter( 1086 const TemplateSpecializationType *T, 1087 raw_ostream &OS) { } 1088 1089 void TypePrinter::printInjectedClassNameBefore(const InjectedClassNameType *T, 1090 raw_ostream &OS) { 1091 printTemplateSpecializationBefore(T->getInjectedTST(), OS); 1092 } 1093 void TypePrinter::printInjectedClassNameAfter(const InjectedClassNameType *T, 1094 raw_ostream &OS) { } 1095 1096 void TypePrinter::printElaboratedBefore(const ElaboratedType *T, 1097 raw_ostream &OS) { 1098 // The tag definition will take care of these. 1099 if (!Policy.IncludeTagDefinition) 1100 { 1101 OS << TypeWithKeyword::getKeywordName(T->getKeyword()); 1102 if (T->getKeyword() != ETK_None) 1103 OS << " "; 1104 NestedNameSpecifier* Qualifier = T->getQualifier(); 1105 if (Qualifier) 1106 Qualifier->print(OS, Policy); 1107 } 1108 1109 ElaboratedTypePolicyRAII PolicyRAII(Policy); 1110 printBefore(T->getNamedType(), OS); 1111 } 1112 void TypePrinter::printElaboratedAfter(const ElaboratedType *T, 1113 raw_ostream &OS) { 1114 ElaboratedTypePolicyRAII PolicyRAII(Policy); 1115 printAfter(T->getNamedType(), OS); 1116 } 1117 1118 void TypePrinter::printParenBefore(const ParenType *T, raw_ostream &OS) { 1119 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) { 1120 printBefore(T->getInnerType(), OS); 1121 OS << '('; 1122 } else 1123 printBefore(T->getInnerType(), OS); 1124 } 1125 void TypePrinter::printParenAfter(const ParenType *T, raw_ostream &OS) { 1126 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) { 1127 OS << ')'; 1128 printAfter(T->getInnerType(), OS); 1129 } else 1130 printAfter(T->getInnerType(), OS); 1131 } 1132 1133 void TypePrinter::printDependentNameBefore(const DependentNameType *T, 1134 raw_ostream &OS) { 1135 OS << TypeWithKeyword::getKeywordName(T->getKeyword()); 1136 if (T->getKeyword() != ETK_None) 1137 OS << " "; 1138 1139 T->getQualifier()->print(OS, Policy); 1140 1141 OS << T->getIdentifier()->getName(); 1142 spaceBeforePlaceHolder(OS); 1143 } 1144 void TypePrinter::printDependentNameAfter(const DependentNameType *T, 1145 raw_ostream &OS) { } 1146 1147 void TypePrinter::printDependentTemplateSpecializationBefore( 1148 const DependentTemplateSpecializationType *T, raw_ostream &OS) { 1149 IncludeStrongLifetimeRAII Strong(Policy); 1150 1151 OS << TypeWithKeyword::getKeywordName(T->getKeyword()); 1152 if (T->getKeyword() != ETK_None) 1153 OS << " "; 1154 1155 if (T->getQualifier()) 1156 T->getQualifier()->print(OS, Policy); 1157 OS << T->getIdentifier()->getName(); 1158 TemplateSpecializationType::PrintTemplateArgumentList(OS, 1159 T->template_arguments(), 1160 Policy); 1161 spaceBeforePlaceHolder(OS); 1162 } 1163 void TypePrinter::printDependentTemplateSpecializationAfter( 1164 const DependentTemplateSpecializationType *T, raw_ostream &OS) { } 1165 1166 void TypePrinter::printPackExpansionBefore(const PackExpansionType *T, 1167 raw_ostream &OS) { 1168 printBefore(T->getPattern(), OS); 1169 } 1170 void TypePrinter::printPackExpansionAfter(const PackExpansionType *T, 1171 raw_ostream &OS) { 1172 printAfter(T->getPattern(), OS); 1173 OS << "..."; 1174 } 1175 1176 void TypePrinter::printAttributedBefore(const AttributedType *T, 1177 raw_ostream &OS) { 1178 // Prefer the macro forms of the GC and ownership qualifiers. 1179 if (T->getAttrKind() == AttributedType::attr_objc_gc || 1180 T->getAttrKind() == AttributedType::attr_objc_ownership) 1181 return printBefore(T->getEquivalentType(), OS); 1182 1183 if (T->getAttrKind() == AttributedType::attr_objc_kindof) 1184 OS << "__kindof "; 1185 1186 printBefore(T->getModifiedType(), OS); 1187 1188 if (T->isMSTypeSpec()) { 1189 switch (T->getAttrKind()) { 1190 default: return; 1191 case AttributedType::attr_ptr32: OS << " __ptr32"; break; 1192 case AttributedType::attr_ptr64: OS << " __ptr64"; break; 1193 case AttributedType::attr_sptr: OS << " __sptr"; break; 1194 case AttributedType::attr_uptr: OS << " __uptr"; break; 1195 } 1196 spaceBeforePlaceHolder(OS); 1197 } 1198 1199 // Print nullability type specifiers. 1200 if (T->getAttrKind() == AttributedType::attr_nonnull || 1201 T->getAttrKind() == AttributedType::attr_nullable || 1202 T->getAttrKind() == AttributedType::attr_null_unspecified) { 1203 if (T->getAttrKind() == AttributedType::attr_nonnull) 1204 OS << " _Nonnull"; 1205 else if (T->getAttrKind() == AttributedType::attr_nullable) 1206 OS << " _Nullable"; 1207 else if (T->getAttrKind() == AttributedType::attr_null_unspecified) 1208 OS << " _Null_unspecified"; 1209 else 1210 llvm_unreachable("unhandled nullability"); 1211 spaceBeforePlaceHolder(OS); 1212 } 1213 } 1214 1215 void TypePrinter::printAttributedAfter(const AttributedType *T, 1216 raw_ostream &OS) { 1217 // Prefer the macro forms of the GC and ownership qualifiers. 1218 if (T->getAttrKind() == AttributedType::attr_objc_gc || 1219 T->getAttrKind() == AttributedType::attr_objc_ownership) 1220 return printAfter(T->getEquivalentType(), OS); 1221 1222 if (T->getAttrKind() == AttributedType::attr_objc_kindof) 1223 return; 1224 1225 // TODO: not all attributes are GCC-style attributes. 1226 if (T->isMSTypeSpec()) 1227 return; 1228 1229 // Nothing to print after. 1230 if (T->getAttrKind() == AttributedType::attr_nonnull || 1231 T->getAttrKind() == AttributedType::attr_nullable || 1232 T->getAttrKind() == AttributedType::attr_null_unspecified) 1233 return printAfter(T->getModifiedType(), OS); 1234 1235 // If this is a calling convention attribute, don't print the implicit CC from 1236 // the modified type. 1237 SaveAndRestore<bool> MaybeSuppressCC(InsideCCAttribute, T->isCallingConv()); 1238 1239 printAfter(T->getModifiedType(), OS); 1240 1241 // Don't print the inert __unsafe_unretained attribute at all. 1242 if (T->getAttrKind() == AttributedType::attr_objc_inert_unsafe_unretained) 1243 return; 1244 1245 // Print nullability type specifiers that occur after 1246 if (T->getAttrKind() == AttributedType::attr_nonnull || 1247 T->getAttrKind() == AttributedType::attr_nullable || 1248 T->getAttrKind() == AttributedType::attr_null_unspecified) { 1249 if (T->getAttrKind() == AttributedType::attr_nonnull) 1250 OS << " _Nonnull"; 1251 else if (T->getAttrKind() == AttributedType::attr_nullable) 1252 OS << " _Nullable"; 1253 else if (T->getAttrKind() == AttributedType::attr_null_unspecified) 1254 OS << " _Null_unspecified"; 1255 else 1256 llvm_unreachable("unhandled nullability"); 1257 1258 return; 1259 } 1260 1261 OS << " __attribute__(("; 1262 switch (T->getAttrKind()) { 1263 default: llvm_unreachable("This attribute should have been handled already"); 1264 case AttributedType::attr_address_space: 1265 OS << "address_space("; 1266 OS << T->getEquivalentType().getAddressSpace(); 1267 OS << ')'; 1268 break; 1269 1270 case AttributedType::attr_vector_size: { 1271 OS << "__vector_size__("; 1272 if (const VectorType *vector =T->getEquivalentType()->getAs<VectorType>()) { 1273 OS << vector->getNumElements(); 1274 OS << " * sizeof("; 1275 print(vector->getElementType(), OS, StringRef()); 1276 OS << ')'; 1277 } 1278 OS << ')'; 1279 break; 1280 } 1281 1282 case AttributedType::attr_neon_vector_type: 1283 case AttributedType::attr_neon_polyvector_type: { 1284 if (T->getAttrKind() == AttributedType::attr_neon_vector_type) 1285 OS << "neon_vector_type("; 1286 else 1287 OS << "neon_polyvector_type("; 1288 const VectorType *vector = T->getEquivalentType()->getAs<VectorType>(); 1289 OS << vector->getNumElements(); 1290 OS << ')'; 1291 break; 1292 } 1293 1294 case AttributedType::attr_regparm: { 1295 // FIXME: When Sema learns to form this AttributedType, avoid printing the 1296 // attribute again in printFunctionProtoAfter. 1297 OS << "regparm("; 1298 QualType t = T->getEquivalentType(); 1299 while (!t->isFunctionType()) 1300 t = t->getPointeeType(); 1301 OS << t->getAs<FunctionType>()->getRegParmType(); 1302 OS << ')'; 1303 break; 1304 } 1305 1306 case AttributedType::attr_objc_gc: { 1307 OS << "objc_gc("; 1308 1309 QualType tmp = T->getEquivalentType(); 1310 while (tmp.getObjCGCAttr() == Qualifiers::GCNone) { 1311 QualType next = tmp->getPointeeType(); 1312 if (next == tmp) break; 1313 tmp = next; 1314 } 1315 1316 if (tmp.isObjCGCWeak()) 1317 OS << "weak"; 1318 else 1319 OS << "strong"; 1320 OS << ')'; 1321 break; 1322 } 1323 1324 case AttributedType::attr_objc_ownership: 1325 OS << "objc_ownership("; 1326 switch (T->getEquivalentType().getObjCLifetime()) { 1327 case Qualifiers::OCL_None: llvm_unreachable("no ownership!"); 1328 case Qualifiers::OCL_ExplicitNone: OS << "none"; break; 1329 case Qualifiers::OCL_Strong: OS << "strong"; break; 1330 case Qualifiers::OCL_Weak: OS << "weak"; break; 1331 case Qualifiers::OCL_Autoreleasing: OS << "autoreleasing"; break; 1332 } 1333 OS << ')'; 1334 break; 1335 1336 // FIXME: When Sema learns to form this AttributedType, avoid printing the 1337 // attribute again in printFunctionProtoAfter. 1338 case AttributedType::attr_noreturn: OS << "noreturn"; break; 1339 1340 case AttributedType::attr_cdecl: OS << "cdecl"; break; 1341 case AttributedType::attr_fastcall: OS << "fastcall"; break; 1342 case AttributedType::attr_stdcall: OS << "stdcall"; break; 1343 case AttributedType::attr_thiscall: OS << "thiscall"; break; 1344 case AttributedType::attr_swiftcall: OS << "swiftcall"; break; 1345 case AttributedType::attr_vectorcall: OS << "vectorcall"; break; 1346 case AttributedType::attr_pascal: OS << "pascal"; break; 1347 case AttributedType::attr_ms_abi: OS << "ms_abi"; break; 1348 case AttributedType::attr_sysv_abi: OS << "sysv_abi"; break; 1349 case AttributedType::attr_regcall: OS << "regcall"; break; 1350 case AttributedType::attr_pcs: 1351 case AttributedType::attr_pcs_vfp: { 1352 OS << "pcs("; 1353 QualType t = T->getEquivalentType(); 1354 while (!t->isFunctionType()) 1355 t = t->getPointeeType(); 1356 OS << (t->getAs<FunctionType>()->getCallConv() == CC_AAPCS ? 1357 "\"aapcs\"" : "\"aapcs-vfp\""); 1358 OS << ')'; 1359 break; 1360 } 1361 case AttributedType::attr_inteloclbicc: OS << "inteloclbicc"; break; 1362 case AttributedType::attr_preserve_most: 1363 OS << "preserve_most"; 1364 break; 1365 case AttributedType::attr_preserve_all: 1366 OS << "preserve_all"; 1367 break; 1368 } 1369 OS << "))"; 1370 } 1371 1372 void TypePrinter::printObjCInterfaceBefore(const ObjCInterfaceType *T, 1373 raw_ostream &OS) { 1374 OS << T->getDecl()->getName(); 1375 spaceBeforePlaceHolder(OS); 1376 } 1377 void TypePrinter::printObjCInterfaceAfter(const ObjCInterfaceType *T, 1378 raw_ostream &OS) { } 1379 1380 void TypePrinter::printObjCTypeParamBefore(const ObjCTypeParamType *T, 1381 raw_ostream &OS) { 1382 OS << T->getDecl()->getName(); 1383 if (!T->qual_empty()) { 1384 bool isFirst = true; 1385 OS << '<'; 1386 for (const auto *I : T->quals()) { 1387 if (isFirst) 1388 isFirst = false; 1389 else 1390 OS << ','; 1391 OS << I->getName(); 1392 } 1393 OS << '>'; 1394 } 1395 1396 spaceBeforePlaceHolder(OS); 1397 } 1398 1399 void TypePrinter::printObjCTypeParamAfter(const ObjCTypeParamType *T, 1400 raw_ostream &OS) { } 1401 1402 void TypePrinter::printObjCObjectBefore(const ObjCObjectType *T, 1403 raw_ostream &OS) { 1404 if (T->qual_empty() && T->isUnspecializedAsWritten() && 1405 !T->isKindOfTypeAsWritten()) 1406 return printBefore(T->getBaseType(), OS); 1407 1408 if (T->isKindOfTypeAsWritten()) 1409 OS << "__kindof "; 1410 1411 print(T->getBaseType(), OS, StringRef()); 1412 1413 if (T->isSpecializedAsWritten()) { 1414 bool isFirst = true; 1415 OS << '<'; 1416 for (auto typeArg : T->getTypeArgsAsWritten()) { 1417 if (isFirst) 1418 isFirst = false; 1419 else 1420 OS << ","; 1421 1422 print(typeArg, OS, StringRef()); 1423 } 1424 OS << '>'; 1425 } 1426 1427 if (!T->qual_empty()) { 1428 bool isFirst = true; 1429 OS << '<'; 1430 for (const auto *I : T->quals()) { 1431 if (isFirst) 1432 isFirst = false; 1433 else 1434 OS << ','; 1435 OS << I->getName(); 1436 } 1437 OS << '>'; 1438 } 1439 1440 spaceBeforePlaceHolder(OS); 1441 } 1442 void TypePrinter::printObjCObjectAfter(const ObjCObjectType *T, 1443 raw_ostream &OS) { 1444 if (T->qual_empty() && T->isUnspecializedAsWritten() && 1445 !T->isKindOfTypeAsWritten()) 1446 return printAfter(T->getBaseType(), OS); 1447 } 1448 1449 void TypePrinter::printObjCObjectPointerBefore(const ObjCObjectPointerType *T, 1450 raw_ostream &OS) { 1451 printBefore(T->getPointeeType(), OS); 1452 1453 // If we need to print the pointer, print it now. 1454 if (!T->isObjCIdType() && !T->isObjCQualifiedIdType() && 1455 !T->isObjCClassType() && !T->isObjCQualifiedClassType()) { 1456 if (HasEmptyPlaceHolder) 1457 OS << ' '; 1458 OS << '*'; 1459 } 1460 } 1461 void TypePrinter::printObjCObjectPointerAfter(const ObjCObjectPointerType *T, 1462 raw_ostream &OS) { } 1463 1464 void TemplateSpecializationType:: 1465 PrintTemplateArgumentList(raw_ostream &OS, 1466 const TemplateArgumentListInfo &Args, 1467 const PrintingPolicy &Policy) { 1468 return PrintTemplateArgumentList(OS, 1469 Args.arguments(), 1470 Policy); 1471 } 1472 1473 void TemplateSpecializationType::PrintTemplateArgumentList( 1474 raw_ostream &OS, ArrayRef<TemplateArgument> Args, 1475 const PrintingPolicy &Policy, bool SkipBrackets) { 1476 const char *Comma = Policy.MSVCFormatting ? "," : ", "; 1477 if (!SkipBrackets) 1478 OS << '<'; 1479 1480 bool needSpace = false; 1481 bool FirstArg = true; 1482 for (const TemplateArgument &Arg : Args) { 1483 // Print the argument into a string. 1484 SmallString<128> Buf; 1485 llvm::raw_svector_ostream ArgOS(Buf); 1486 if (Arg.getKind() == TemplateArgument::Pack) { 1487 if (Arg.pack_size() && !FirstArg) 1488 OS << Comma; 1489 PrintTemplateArgumentList(ArgOS, 1490 Arg.getPackAsArray(), 1491 Policy, true); 1492 } else { 1493 if (!FirstArg) 1494 OS << Comma; 1495 Arg.print(Policy, ArgOS); 1496 } 1497 StringRef ArgString = ArgOS.str(); 1498 1499 // If this is the first argument and its string representation 1500 // begins with the global scope specifier ('::foo'), add a space 1501 // to avoid printing the diagraph '<:'. 1502 if (FirstArg && !ArgString.empty() && ArgString[0] == ':') 1503 OS << ' '; 1504 1505 OS << ArgString; 1506 1507 needSpace = (!ArgString.empty() && ArgString.back() == '>'); 1508 FirstArg = false; 1509 } 1510 1511 // If the last character of our string is '>', add another space to 1512 // keep the two '>''s separate tokens. We don't *have* to do this in 1513 // C++0x, but it's still good hygiene. 1514 if (needSpace) 1515 OS << ' '; 1516 1517 if (!SkipBrackets) 1518 OS << '>'; 1519 } 1520 1521 // Sadly, repeat all that with TemplateArgLoc. 1522 void TemplateSpecializationType:: 1523 PrintTemplateArgumentList(raw_ostream &OS, 1524 ArrayRef<TemplateArgumentLoc> Args, 1525 const PrintingPolicy &Policy) { 1526 OS << '<'; 1527 const char *Comma = Policy.MSVCFormatting ? "," : ", "; 1528 1529 bool needSpace = false; 1530 bool FirstArg = true; 1531 for (const TemplateArgumentLoc &Arg : Args) { 1532 if (!FirstArg) 1533 OS << Comma; 1534 1535 // Print the argument into a string. 1536 SmallString<128> Buf; 1537 llvm::raw_svector_ostream ArgOS(Buf); 1538 if (Arg.getArgument().getKind() == TemplateArgument::Pack) { 1539 PrintTemplateArgumentList(ArgOS, 1540 Arg.getArgument().getPackAsArray(), 1541 Policy, true); 1542 } else { 1543 Arg.getArgument().print(Policy, ArgOS); 1544 } 1545 StringRef ArgString = ArgOS.str(); 1546 1547 // If this is the first argument and its string representation 1548 // begins with the global scope specifier ('::foo'), add a space 1549 // to avoid printing the diagraph '<:'. 1550 if (FirstArg && !ArgString.empty() && ArgString[0] == ':') 1551 OS << ' '; 1552 1553 OS << ArgString; 1554 1555 needSpace = (!ArgString.empty() && ArgString.back() == '>'); 1556 FirstArg = false; 1557 } 1558 1559 // If the last character of our string is '>', add another space to 1560 // keep the two '>''s separate tokens. We don't *have* to do this in 1561 // C++0x, but it's still good hygiene. 1562 if (needSpace) 1563 OS << ' '; 1564 1565 OS << '>'; 1566 } 1567 1568 std::string Qualifiers::getAsString() const { 1569 LangOptions LO; 1570 return getAsString(PrintingPolicy(LO)); 1571 } 1572 1573 // Appends qualifiers to the given string, separated by spaces. Will 1574 // prefix a space if the string is non-empty. Will not append a final 1575 // space. 1576 std::string Qualifiers::getAsString(const PrintingPolicy &Policy) const { 1577 SmallString<64> Buf; 1578 llvm::raw_svector_ostream StrOS(Buf); 1579 print(StrOS, Policy); 1580 return StrOS.str(); 1581 } 1582 1583 bool Qualifiers::isEmptyWhenPrinted(const PrintingPolicy &Policy) const { 1584 if (getCVRQualifiers()) 1585 return false; 1586 1587 if (getAddressSpace()) 1588 return false; 1589 1590 if (getObjCGCAttr()) 1591 return false; 1592 1593 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) 1594 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime)) 1595 return false; 1596 1597 return true; 1598 } 1599 1600 // Appends qualifiers to the given string, separated by spaces. Will 1601 // prefix a space if the string is non-empty. Will not append a final 1602 // space. 1603 void Qualifiers::print(raw_ostream &OS, const PrintingPolicy& Policy, 1604 bool appendSpaceIfNonEmpty) const { 1605 bool addSpace = false; 1606 1607 unsigned quals = getCVRQualifiers(); 1608 if (quals) { 1609 AppendTypeQualList(OS, quals, Policy.Restrict); 1610 addSpace = true; 1611 } 1612 if (hasUnaligned()) { 1613 if (addSpace) 1614 OS << ' '; 1615 OS << "__unaligned"; 1616 addSpace = true; 1617 } 1618 if (unsigned addrspace = getAddressSpace()) { 1619 if (addSpace) 1620 OS << ' '; 1621 addSpace = true; 1622 switch (addrspace) { 1623 case LangAS::opencl_global: 1624 OS << "__global"; 1625 break; 1626 case LangAS::opencl_local: 1627 OS << "__local"; 1628 break; 1629 case LangAS::opencl_constant: 1630 OS << "__constant"; 1631 break; 1632 case LangAS::opencl_generic: 1633 OS << "__generic"; 1634 break; 1635 default: 1636 OS << "__attribute__((address_space("; 1637 OS << addrspace; 1638 OS << ")))"; 1639 } 1640 } 1641 if (Qualifiers::GC gc = getObjCGCAttr()) { 1642 if (addSpace) 1643 OS << ' '; 1644 addSpace = true; 1645 if (gc == Qualifiers::Weak) 1646 OS << "__weak"; 1647 else 1648 OS << "__strong"; 1649 } 1650 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) { 1651 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime)){ 1652 if (addSpace) 1653 OS << ' '; 1654 addSpace = true; 1655 } 1656 1657 switch (lifetime) { 1658 case Qualifiers::OCL_None: llvm_unreachable("none but true"); 1659 case Qualifiers::OCL_ExplicitNone: OS << "__unsafe_unretained"; break; 1660 case Qualifiers::OCL_Strong: 1661 if (!Policy.SuppressStrongLifetime) 1662 OS << "__strong"; 1663 break; 1664 1665 case Qualifiers::OCL_Weak: OS << "__weak"; break; 1666 case Qualifiers::OCL_Autoreleasing: OS << "__autoreleasing"; break; 1667 } 1668 } 1669 1670 if (appendSpaceIfNonEmpty && addSpace) 1671 OS << ' '; 1672 } 1673 1674 std::string QualType::getAsString(const PrintingPolicy &Policy) const { 1675 std::string S; 1676 getAsStringInternal(S, Policy); 1677 return S; 1678 } 1679 1680 std::string QualType::getAsString(const Type *ty, Qualifiers qs) { 1681 std::string buffer; 1682 LangOptions options; 1683 getAsStringInternal(ty, qs, buffer, PrintingPolicy(options)); 1684 return buffer; 1685 } 1686 1687 void QualType::print(const Type *ty, Qualifiers qs, 1688 raw_ostream &OS, const PrintingPolicy &policy, 1689 const Twine &PlaceHolder, unsigned Indentation) { 1690 SmallString<128> PHBuf; 1691 StringRef PH = PlaceHolder.toStringRef(PHBuf); 1692 1693 TypePrinter(policy, Indentation).print(ty, qs, OS, PH); 1694 } 1695 1696 void QualType::getAsStringInternal(const Type *ty, Qualifiers qs, 1697 std::string &buffer, 1698 const PrintingPolicy &policy) { 1699 SmallString<256> Buf; 1700 llvm::raw_svector_ostream StrOS(Buf); 1701 TypePrinter(policy).print(ty, qs, StrOS, buffer); 1702 std::string str = StrOS.str(); 1703 buffer.swap(str); 1704 } 1705