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